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. Advertisements

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:
    1,076
    Rob Schieber
    Sep 30, 2005
  2. Michal Maciejczak
    Replies:
    1
    Views:
    2,153
    Matthias Kaeppler
    Apr 23, 2005
  3. Christos TZOTZIOY Georgiou
    Replies:
    3
    Views:
    1,020
    Christos TZOTZIOY Georgiou
    Sep 13, 2003
  4. mp
    Replies:
    1
    Views:
    661
    John Machin
    Jul 28, 2006
  5. zamba
    Replies:
    2
    Views:
    394
  6. Martin
    Replies:
    0
    Views:
    625
    Martin
    Dec 27, 2008
  7. Replies:
    2
    Views:
    1,048
    M.-A. Lemburg
    Jan 6, 2009
  8. PW

    Dates! Dates! Dates!

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