How do I calculate number of days betwen two dates using Python

De openkb
Aller à : Navigation, rechercher

Sommaire

Questions

If I have two dates (ex. 8/18/2008 and 9/26/2008 ) what is the best way to get the difference measured in days?

Answers

If you have two date objects, you can just subtract them.

from datetime import date

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

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

Source

License : cc by-sa 3.0

http://stackoverflow.com/questions/151199/how-do-i-calculate-number-of-days-betwen-two-dates-using-python

Related

Outils personnels
Espaces de noms

Variantes
Actions
Navigation
Outils