datetime: How to get diff between 2 dates in month units?
niemeyer at conectiva.com
Mon Sep 15 02:06:31 CEST 2003
> 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.
If you can rely on external modules, you may use mx.DateTime for that.
> >>> d1 = datetime.date(2002,1,1)
> >>> d2 = datetime.date(2001,12,1)
> >>> diff = d1-d2
> >>> diff.days
This would be:
>>> d1 = DateTime.Date(2002,1,1)
>>> d2 = DateTime.Date(2001,12,1)
>>> diff = DateTime.RelativeDateDiff(d1,d2)
> >>>firstdate = newMonthlyDatetime(2002, 5)
> >>>Nov2002 = newMonthlyDatetime(2002, 11)
> >>>diff = Nov2002 - firstdate
And this would be:
>>> firstdate = DateTime.Date(2002, 5)
>>> Nov2002 = DateTime.Date(2002, 11)
>>> diff = DateTime.RelativeDateDiff(Nov2002, firstdate)
More information about the Python-list