[Tutor] datetime module problem

Dick Moores rdm at rcblue.com
Wed Apr 16 01:31:23 CEST 2008


At 02:37 PM 4/15/2008, Kent Johnson wrote:
>Dick Moores wrote:
>>I'm really struggling with the datetime module. Trying for a script 
>>that will calculate the number of days between any two dates
>
>How about this:
>
>from datetime import datetime
>date1 = raw_input("Enter date1 as year-month-day: ")
>date1 = datetime.strptime(date1, '%Y-%m-%d')
>date2 = raw_input("Enter date2 as year-month-day: ")
>date2 = datetime.strptime(date2, '%Y-%m-%d')
>print "date2 - date1 is", (date2 - date1).days, 'days'
>
>Kent

Yes, thanks, Kent.

I finally tracked down that table for the format string, at 
<http://docs.python.org/lib/module-time.html>. Been experimenting 
with it. Realized that the format string could be the more familiar 
American '%m/%d/%Y', or '%m/%d/%y'.

The docs are so hard for me to understand, that I'm even feeling 
pleased with myself for this:

from datetime import datetime
date1 = raw_input("Enter date1 as year-month-day: ")
date1 = datetime.strptime(date1, '%m/%d/%Y')
today = datetime.now()
print "today - date1 is", (today - date1).days, 'days'

Dick


            ================================
UliPad <<The Python Editor>>: http://code.google.com/p/ulipad/ 



More information about the Tutor mailing list