473,395 Members | 1,473 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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
Sep 22 '07 #1
5 6375
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
Sep 22 '07 #2
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
Sep 22 '07 #3
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

Sep 22 '07 #4
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 )

Sep 22 '07 #5
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!

Sep 23 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Hans Gruber | last post by:
Hi all, I have been struggling with a problem all day, I have been unable to come up with a working solution. I want to write a function which takes 2 unix timestamps and calculates the...
2
by: Alex | last post by:
I need to calculate the number of days between two dates formatted yyyymmdd. Besides creating a whole date class, is there a simpler method of doing it? for example: 20050203 - 20050201 = 2 ...
26
by: Frank | last post by:
For my website i would like to display the age of my son in years, months, days and hours. For now i manage to get a result for totals. Like the total number of days. This is the beginning: ...
4
by: Shyguy | last post by:
I have a database that backs up critical tables to a database, named Backup with the date, daily. I can't figure out how to delete these databases programmatically, say when they are 5 days old or...
5
by: Ali Baba | last post by:
Hi, Is there is equivalent of VB's DateDiff() method in C#. I need to find difference in months between two dates that are years apart. Docs says that I can use TimeSpan like: TimeSpam ts =...
10
by: rob | last post by:
I have a class that among others exposes a string property "Date". The date in this property is stored in the form yyyymmdd. Now I do the following 1) Generate a DataGridViewTextBoxColumn column...
23
by: thebjorn | last post by:
For the purpose of finding someone's age I was looking for a way to find how the difference in years between two dates, so I could do something like: age = (date.today() - born).year but that...
10
by: =?Utf-8?B?TWlrZQ==?= | last post by:
I have a string variable containing a date formatted as YYYYMMDD For example - Dim x as string = "20070314" If I try to perform a type conversion as follows I get an error: Dim y as Date =...
15
by: student4lifer | last post by:
Hello, I have 2 time fields dynamically generated in format "m/d/y H:m". Could someone show me a good function to calculate the time interval difference in minutes? I played with strtotime() but...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.