[Tutor] How Compute # of Days between Two Dates?

William Allison allison.william at gmail.com
Mon Sep 1 16:34:29 CEST 2008


Wayne Watson wrote:
> 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.

I've used the datetime module to do something similar.

Python 2.5.2 (r252:60911, Feb 22 2008, 07:57:53)
[GCC 4.0.1 (Apple Computer, Inc. build 5363)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import datetime
>>> today = datetime.date.today()
>>> print today
2008-09-01
>>> last_year = datetime.date(2007, 9, 1)
>>> print today - last_year
366 days, 0:00:00
>>>

HTH,
Will



More information about the Tutor mailing list