Connecting Tech Pros Worldwide Forums | Help | Site Map

datetime: How to get diff between 2 dates in month units?

python@sarcastic-horse.com
Guest
 
Posts: n/a
#1: Jul 18 '05
Hi-


I have a lot of monthly time series data. I need to be able to compare
two dates and get the number of months that they are apart.

The datetime module is a daily-frequency data type. It has a timedelta
object that returns the difference in days between two dates:
[color=blue][color=green][color=darkred]
>>> d1 = datetime.date(2002,1,1)
>>> d2 = datetime.date(2001,12,1)
>>> diff = d1-d2
>>> diff.days[/color][/color][/color]
31

I want to be able to compare monthly-frequency data, so that I can do
something like this:
[color=blue][color=green][color=darkred]
>>>firstdate = newMonthlyDatetime(2002, 5)
>>>Nov2002 = newMonthlyDatetime(2002, 11)
>>>diff = Nov2002 - firstdate
>>>diff.months[/color][/color][/color]
6

Does anyone have any suggestions about how to do something like this?

Thanks for the help.

Closed Thread