# find difference in days from YYYYMMDD to YYYYMMDD

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

1. ### Konstantinos PachopoulosGuest

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

2. ### Diez B. RoggischGuest

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

3. ### John MachinGuest

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?

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
4. ### 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

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 )

6. ### 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