find difference in days from YYYYMMDD to YYYYMMDD

Discussion in 'Python' started by Konstantinos Pachopoulos, Sep 22, 2007.

  1. Hi,
    does any body now any such algorith? to find difference in days from
    YYYYMMDD to YYYYMMDD?
    Or just an algorithm, that converts YYYYMMDD to seconds since the epoch?

    Thanks
    Konstantinos Pachopoulos, Sep 22, 2007
    #1
    1. Advertising

  2. Konstantinos Pachopoulos schrieb:
    > Hi,
    > does any body now any such algorith? to find difference in days from
    > YYYYMMDD to YYYYMMDD?
    > Or just an algorithm, that converts YYYYMMDD to seconds since the epoch?


    See the modules datetime and time in the standard library.

    Diez
    Diez B. Roggisch, Sep 22, 2007
    #2
    1. Advertising

  3. Konstantinos Pachopoulos

    John Machin Guest

    On 22/09/2007 7:37 PM, Konstantinos Pachopoulos wrote:
    > Hi,
    > does any body now any such algorith? to find difference in days from
    > YYYYMMDD to YYYYMMDD?



    The literal answer to your question is, unsurprisingly, "Yes".

    Now, what do you really want/need:

    (a) details of an algorithm so that you can code it in Python
    (b) an implementation of (a)
    (c) a hint that Python might already have a standard module for date
    calculations
    (d) a hint that there are things called search engines ... just copy
    your subject and paste it in


    > Or just an algorithm, that converts YYYYMMDD to seconds since the epoch?
    >


    There is no such thing as "the" (unqualified) epoch.

    Try days_difference(from_date=your_chosen_epoch, to_date=some_date) * 24
    * 60 * 60
    John Machin, Sep 22, 2007
    #3
  4. Konstantinos Pachopoulos

    Guest

    On 22 sep, 11:37, Konstantinos Pachopoulos <>
    wrote:

    > does any body now any such algorith? to find difference in days from
    > YYYYMMDD to YYYYMMDD?


    Once I needed the same and I wrote:

    def days_difference(s1, s2):
    splitdate = lambda s: time.strptime(s, "%Y%m%d")[:3]
    str2date = lambda s: datetime.date(*splitdate(s))
    delta = str2date(s1) - str2date(s2)
    return delta.days

    print days_difference("20071112", "20061029") # 379

    Although I'm sure there is a better way.

    arnau
    , Sep 22, 2007
    #4
  5. Konstantinos Pachopoulos

    Zentrader Guest

    On Sep 22, 2:37 am, Konstantinos Pachopoulos <>
    wrote:
    > Hi,
    > does any body now any such algorith? to find difference in days from
    > YYYYMMDD to YYYYMMDD?
    > Or just an algorithm, that converts YYYYMMDD to seconds since the epoch?
    >
    > Thanks


    For some reason, to-seconds-since-epoch is in the calendar class.
    calendar.timegm() takes a tuple and returns the epoch seconds
    import time
    import calendar

    today_secs = calendar.timegm( (2007, 9, 22, 0, 0, 0) )
    print today_secs
    one_day = 24*60*60
    print time.gmtime( today_secs+one_day )
    Zentrader, Sep 22, 2007
    #5
  6. Konstantinos Pachopoulos

    Guest

    On Sep 22, 5:37 am, Konstantinos Pachopoulos <>
    wrote:
    > Hi,
    > does any body now any such algorith? to find difference in days from
    > YYYYMMDD to YYYYMMDD?
    > Or just an algorithm, that converts YYYYMMDD to seconds since the epoch?
    >
    > Thanks


    Seen some complex answers here. Let's keep it dead simple. Use the
    datetime module to do the heavy lifting. Go to IDLE, write this.

    import datetime.

    Then start to write this:

    difference = datetime.date(

    and at that point IDLE will tell you to put in year,month,day. That's
    convenient. Do as IDLE asks, obey IDLE! Wind up with this (put date
    later in history first, here are two I've used):

    difference = datetime.date(2007,9,25) - datetime.date(1970,12,25)
    print difference

    13419 days, 0:00:00

    Hey now! Date math! Yeah!
    , Sep 23, 2007
    #6
    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. Phil Hanna
    Replies:
    3
    Views:
    15,430
    Keeger
    Jul 1, 2003
  2. David Stockwell
    Replies:
    2
    Views:
    700
    Anna Martelli Ravenscroft
    Sep 20, 2004
  3. Gerrit
    Replies:
    0
    Views:
    385
    Gerrit
    Sep 20, 2004
  4. Terry Reedy
    Replies:
    0
    Views:
    426
    Terry Reedy
    Sep 20, 2004
  5. Replies:
    5
    Views:
    462
    Bo Yang
    Nov 2, 2006
Loading...

Share This Page