473,320 Members | 1,950 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,320 software developers and data experts.

How Compute # of Days between Two Dates?

That's the question in Subject. For example, the difference between
08/29/2008 and 09/03/2008 is +5. The difference between 02/28/2008 and
03/03/2008 is 4, leap year--extra day in Feb. I'm really only interested in
years between, say, 1990 and 2050. In other words not some really strange
period of time well outside our current era of history.

--
W. eWatson

(121.015 Deg. W, 39.262 Deg. N) GMT-8 hr std. time)
Obz Site: 39° 15' 7" N, 121° 2' 32" W, 2700 feet

Web Page: <www.speckledwithstars.net/>

Sep 1 '08 #1
6 3518
from datetime import datetime

# batteries included

today = datetime.now()
xmas = datetime(today.year,12,25)
if (xmas - today).days 1:
print "%d days until Christmas" % (xmas - today).days
else:
print "Merry Christmas!"
Sep 1 '08 #2
On 2008-09-01, W. eWatson <no*******@sbcglobal.netwrote:
That's the question in Subject. For example, the difference between
08/29/2008 and 09/03/2008 is +5. The difference between 02/28/2008 and
03/03/2008 is 4, leap year--extra day in Feb. I'm really only interested in
years between, say, 1990 and 2050. In other words not some really strange
period of time well outside our current era of history.
Does the standard library's datetime module not do what you want?

http://docs.python.org/lib/module-datetime.html

--
Grant
Sep 1 '08 #3
Grant Edwards wrote:
On 2008-09-01, W. eWatson <no*******@sbcglobal.netwrote:
>That's the question in Subject. For example, the difference between
08/29/2008 and 09/03/2008 is +5. The difference between 02/28/2008 and
03/03/2008 is 4, leap year--extra day in Feb. I'm really only interested in
years between, say, 1990 and 2050. In other words not some really strange
period of time well outside our current era of history.

Does the standard library's datetime module not do what you want?

http://docs.python.org/lib/module-datetime.html
Yes, it would seem so. This works fine.

date1 = datetime.date(2007, 2, 27)
date2 = datetime.date(2007, 3, 3)

print "date1: ", date1
print "date2: ", date2
diff = date2 - date1
print "diff: ", diff
result:
date1: 2007-02-27
date2: 2007-03-03
diff: 4 days, 0:00:00

I was pondering this in pyfdate, but perhaps missed it or it was not obvious
to me in the tutorial for some reason. There are few places where it's not
quite complete. pyfdate has some rules for dealing with length of month
oddities that started me thinking it would have difficulty with situations
like the above. However, it would seem any general implementation of time
and date should be capable of making similar calculations without difficulty.

--
W. eWatson

(121.015 Deg. W, 39.262 Deg. N) GMT-8 hr std. time)
Obz Site: 39° 15' 7" N, 121° 2' 32" W, 2700 feet

Web Page: <www.speckledwithstars.net/>

Sep 1 '08 #4
On 2008-09-01, W. eWatson <no*******@sbcglobal.netwrote:
Grant Edwards wrote:
>On 2008-09-01, W. eWatson <no*******@sbcglobal.netwrote:
>>That's the question in Subject. For example, the difference between
08/29/2008 and 09/03/2008 is +5. The difference between 02/28/2008 and
03/03/2008 is 4, leap year--extra day in Feb. I'm really only interested in
years between, say, 1990 and 2050. In other words not some really strange
period of time well outside our current era of history.

Does the standard library's datetime module not do what you want?

http://docs.python.org/lib/module-datetime.html
Yes, it would seem so. This works fine.
It would probably be worth your while to read through one of
introductory Python books or just browse through the Python
tutorial:

http://docs.python.org/tut/
I was pondering this in pyfdate, but perhaps missed it or it
was not obvious to me in the tutorial for some reason.
Sorry, can't help you there -- I've never heard of pyfdate. The
timedate module that comes with Python has always done what I
needed to do with dates/times.

--
Grant

Sep 1 '08 #5
Grant Edwards wrote:
On 2008-09-01, W. eWatson <no*******@sbcglobal.netwrote:
>Grant Edwards wrote:
>>On 2008-09-01, W. eWatson <no*******@sbcglobal.netwrote:

That's the question in Subject. For example, the difference between
08/29/2008 and 09/03/2008 is +5. The difference between 02/28/2008 and
03/03/2008 is 4, leap year--extra day in Feb. I'm really only interested in
years between, say, 1990 and 2050. In other words not some really strange
period of time well outside our current era of history.
Does the standard library's datetime module not do what you want?

http://docs.python.org/lib/module-datetime.html
Yes, it would seem so. This works fine.

It would probably be worth your while to read through one of
introductory Python books or just browse through the Python
tutorial:

http://docs.python.org/tut/
Oddly, Leaning Python has no mention of datetime (not date or time), at
least, that I could find. I'm considering the Nutshell book, 2nd ed., as a
better reference (and cross reference) to various topics.
>
>I was pondering this in pyfdate, but perhaps missed it or it
was not obvious to me in the tutorial for some reason.

Sorry, can't help you there -- I've never heard of pyfdate. The
timedate module that comes with Python has always done what I
needed to do with dates/times.

--
W. eWatson

(121.015 Deg. W, 39.262 Deg. N) GMT-8 hr std. time)
Obz Site: 39° 15' 7" N, 121° 2' 32" W, 2700 feet

Web Page: <www.speckledwithstars.net/>

Sep 1 '08 #6
On 2008-09-01, W. eWatson <no*******@sbcglobal.netwrote:
Oddly, Leaning Python has no mention of datetime (not date or time), at
least, that I could find. I'm considering the Nutshell book, 2nd ed., as a
better reference (and cross reference) to various topics.
datetime is pretty new standard library module. I'm quite sure that
it wasn't around when the latest edition of Learning Python was written.

--
Ari Makela late autumn -
ha***@arska.org a single chair waiting
http://arska.org/hauva/ for someone yet to come
-- Arima Akito
Sep 1 '08 #7

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

Similar topics

2
by: David Mitchell | last post by:
I have tried using the following code to count the specific number of each weekday but get a compile error "User defined type not defined" which I think relates to the first line of the function: -...
8
by: Mojca | last post by:
What is formula that get days between two dates? Npr: 11.03.1998 – 1.7.2005 Thank you, Mojca
1
by: rock72 | last post by:
I am developing a application using this fields as required by the company. 1. Date Login 2. Time IN 3. Time OUT My question is how to produce the number of days with 1 entry of Date is...
4
by: Russell | last post by:
I have an assignment that I have to complete. I have to write a windows app in C#. Here is the spec: 1/ Date Comparison Build a program that will find the number of days between two dates. You...
29
by: james | last post by:
I have a problem that at first glance seems not that hard to figure out. But, so far, the answer has escaped me. I have an old database file that has the date(s) stored in it as number of days. An...
2
by: bufbec | last post by:
I have worked on this for hours and can't come up with a solution. Hope someone can help me. I have a table called TMBS_HMAUDIT_PARMS. this table contains data to tell me how often a person is...
2
gchq
by: gchq | last post by:
Hi there I am attempting to compute some values from a DataTable. It works fine as long as there is a value to retrieve, otherwise it blows out! This will work as there are valid dates:- ...
2
by: angi35 | last post by:
Hi, I'm working in Access 2000. I have a form with a series of date fields, showing the progress of a project from start to completion. There's a set of fields/controls for projected dates (when...
5
FishVal
by: FishVal | last post by:
IMHO, the following is not a how-to-do instruction to solve a particular problem but more a concept-proof stuff demonstrating possibilities of SQL. So, let us say the problem is to calculate...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.