Velocity Reviews > find difference in days from YYYYMMDD to YYYYMMDD

# find difference in days from YYYYMMDD to YYYYMMDD

Konstantinos Pachopoulos
Guest
Posts: n/a

 09-22-2007
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

Diez B. Roggisch
Guest
Posts: n/a

 09-22-2007
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

John Machin
Guest
Posts: n/a

 09-22-2007
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

tokland@gmail.com
Guest
Posts: n/a

 09-22-2007
On 22 sep, 11:37, Konstantinos Pachopoulos <(E-Mail Removed)>
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

Guest
Posts: n/a

 09-22-2007
On Sep 22, 2:37 am, Konstantinos Pachopoulos <(E-Mail Removed)>
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 )

chaelon@gmail.com
Guest
Posts: n/a

 09-23-2007
On Sep 22, 5:37 am, Konstantinos Pachopoulos <(E-Mail Removed)>
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!