0

how can I check the difference between two dates using the datetime modules? (or any other module that would do the same thing).
I want to read a date from a file (which was written in the same format as the datetime module uses) and then calculate the difference in days between that date and today.

3
Contributors
5
Replies
7
Views
7 Years
Discussion Span
Last Post by Stefano Mtangoo
0
>>> import datetime
>>> dir(datetime)
['MAXYEAR', 'MINYEAR', '__doc__', '__name__', '__package__', 'date', 'datetime', 'datetime_CAPI', 'time', 'timedelta', 'tzinfo']
>>> #Use method date and timedelta
>>> print datetime.date.today()
2010-01-11
>>>
>>> #Find now days since 1 Jan 1901 
>>> one_day = datetime.timedelta(days=1)
>>> long_time_ago = datetime.date(1901, 1, 01)
>>> print to_day - long_time_ago
39822 days, 0:00:00

Edited by snippsat: n/a

0

I'm afraid I don't understand that example.. what is 'to-day'? It hasn't been defined earlier.

0

thanks for your help. Your link and a little more rummaging in the python documentation worked. It turns out you can just subtract two date objects and that will return a timedelta object.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.