[Transfer] of using python calculate the number of days between two dates ﹑ weeks and several days after the designated date corresponds to

2010-02-02  来源:本站原创  分类:Python  人气:282 

>>> import datetime
>>> help(datetime)
http://docs.python.org/library/datetime.html

 View the 2009  5 January 31, and  2009 2/  1 How many days of the date interval
>>> d1=datetime.date(2009,05,31)
>>> d2=datetime.date(2009,02,01)
>>> d1-d2
datetime.timedelta(119) //  You can see in 2009  5 January 31, and  2009 2/  1 Date interval for 119 days  [luther.gliethttp].

 View the 2009  5 May 31 is the day of the year and week
>>> d1=datetime.date(2009,05,31)
>>> d1.isocalendar()
(2009, 22, 7) // 2009 Years, section  22 Week, week  7 [luther.gliethttp]
>>> d1.timetuple()
(2009, 5, 31, 0, 0, 0, 6, 151, -1) // (d.year, d.month, d.day, d.hour, d.minute, d.second, d.weekday(),  The day of the year  , dst)

 View the 2009  5 After March 31,  20 The date that corresponds to the week
>>> d1=datetime.date(2009,05,31)
>>> d=datetime.timedelta(weeks=20) // 20 The week will be automatically translated into a number of days  [luther.gliethttp]
>>> d1+d
datetime.date(2009, 10, 18) //  Therefore, 2009  5 After March 31,  20 The corresponding week 2009  10 18  [luther.gliethttp]

 View the 2009  5 After March 31,  100 Days of the date and 90 days before the date of
>>> d1=datetime.date(2009,05,31)
>>> d=datetime.timedelta(days=100) // 100 Day
>>> d1+d
datetime.date(2009, 9, 8) //  Therefore, 2009  5 After March 31,  100 Days of the date of 2009  9 On May 8,  [luther.gliethttp]
>>> d=datetime.timedelta(days=90)
>>> d1-d
datetime.date(2009, 3, 2) //  Therefore, 2009  5 The number of 31 may forward  90 Days of the date of 2009  3 2 February  [luther.gliethttp]

 See examples of using small datetime.timedelta
datetime.timedelta([days[, seconds[, microseconds[, milliseconds[, minutes[, hours[, weeks]]]]]]])
 For example,  :
>>> datetime.timedelta(weeks=40, days=84, hours=23, minutes=50, seconds=600)
datetime.timedelta(365) //  Use  timedelta() After various combinations of the total number of days after conversion to 365 days  [luther.gliethttp]
// 40*7 + 84 = 364
// 23 50 minutes when  600 Seconds = 24  , So for 1 day
//  It is ultimately for 364 + 1 = 365 days  [luther.gliethtp].
相关文章