[Tutor] datetime module problem

Kent Johnson kent37 at tds.net
Tue Apr 15 23:37:18 CEST 2008


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


More information about the Tutor mailing list