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

Discussion in 'Python' started by python@sarcastic-horse.com, Sep 12, 2003.

  1. Guest

    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:

    >>> d1 = datetime.date(2002,1,1)
    >>> d2 = datetime.date(2001,12,1)
    >>> diff = d1-d2
    >>> diff.days

    31

    I want to be able to compare monthly-frequency data, so that I can do
    something like this:

    >>>firstdate = newMonthlyDatetime(2002, 5)
    >>>Nov2002 = newMonthlyDatetime(2002, 11)
    >>>diff = Nov2002 - firstdate
    >>>diff.months

    6

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

    Thanks for the help.
    , Sep 12, 2003
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. David Lozzi

    Dates dates dates dates... SQL and ASP.NET

    David Lozzi, Sep 29, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    843
    Rob Schieber
    Sep 30, 2005
  2. Michal Maciejczak
    Replies:
    1
    Views:
    1,970
    Matthias Kaeppler
    Apr 23, 2005
  3. Gustavo Niemeyer
    Replies:
    0
    Views:
    532
    Gustavo Niemeyer
    Sep 15, 2003
  4. zamba
    Replies:
    2
    Views:
    310
  5. PW

    Dates! Dates! Dates!

    PW, Aug 7, 2004, in forum: ASP General
    Replies:
    4
    Views:
    178
    Mark Schupp
    Aug 9, 2004
Loading...

Share This Page