HiveBrain v1.2.0
Get Started
← Back to all entries
snippetpythonCritical

How to calculate number of days between two given dates

Submitted by: @import:stackoverflow-api··
0
Viewed 0 times
calculatehowdaysdatesnumbergiventwobetween

Problem

If I have two dates (ex. '8/18/2008' and '9/26/2008'), what is the best way to get the number of days between these two dates?

Solution

If you have two date objects, you can just subtract them, which computes a timedelta object.

from datetime import date

d0 = date(2008, 8, 18)
d1 = date(2008, 9, 26)
delta = d1 - d0
print(delta.days)


The relevant section of the docs:
https://docs.python.org/library/datetime.html.

See this answer for another example.

Code Snippets

from datetime import date

d0 = date(2008, 8, 18)
d1 = date(2008, 9, 26)
delta = d1 - d0
print(delta.days)

Context

Stack Overflow Q#151199, score: 1238

Revisions (0)

No revisions yet.