[Tutor] datetime module problem
bgailer at gmail.com
Tue Apr 15 23:29:08 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, I've
> come up with this:
> import datetime
> date1 = raw_input("Enter date1 as year-month-day: ")
> year1, month1, day1 = date1.split('-')
> date1 = datetime.date(int(year1), int(month1), int(day1))
> date2 = raw_input("Enter date2 as year-month-day: ")
> year2, month2, day2 = date2.split('-')
> date2 = datetime.date(int(year2), int(month2), int(day2))
> print "date2 - date1 is", date2 - date1
> Here's one run:
> Enter date1 as year-month-day: 2003-4-15
> Enter date2 as year-month-day: 2008-4-15
> date2 - date1 is 1827 days, 0:00:00
> How can I get rid of that "0:00:00"?
> And there must be a better way. What is it?
You might read the datetime documentation.
And then notice that date2 - date1 is a timedelta object.
And then look that up to see its attributes (which inculdes days)
And then try print (date2 - date1).days
919-636-4239 Chapel Hill, NC
More information about the Tutor