Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Python (http://www.velocityreviews.com/forums/f43-python.html)
-   -   find difference in days from YYYYMMDD to YYYYMMDD (http://www.velocityreviews.com/forums/t539491-find-difference-in-days-from-yyyymmdd-to-yyyymmdd.html)

 Konstantinos Pachopoulos 09-22-2007 09:37 AM

find difference in days from YYYYMMDD to YYYYMMDD

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 09-22-2007 10:37 AM

Re: find difference in days from YYYYMMDD to YYYYMMDD

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 09-22-2007 10:50 AM

Re: find difference in days from YYYYMMDD to YYYYMMDD

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 09-22-2007 03:04 PM

Re: find difference in days from YYYYMMDD to YYYYMMDD

On 22 sep, 11:37, Konstantinos Pachopoulos <kostaspa...@yahoo.gr>
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

Re: find difference in days from YYYYMMDD to YYYYMMDD

On Sep 22, 2:37 am, Konstantinos Pachopoulos <kostaspa...@yahoo.gr>
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 09-23-2007 01:09 AM

Re: find difference in days from YYYYMMDD to YYYYMMDD

On Sep 22, 5:37 am, Konstantinos Pachopoulos <kostaspa...@yahoo.gr>
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!

 All times are GMT. The time now is 02:21 PM.