开发者

Python get start and end date of the week

How to get the s开发者_StackOverflow中文版tart date and end date of a week, and also if the following dates below comes under a particular range of start and end dates of the week how to show only those weeks start and end date.I am using python 2.4

2011-03-07  0:27:41
2011-03-06  0:13:41
2011-03-05  0:17:40
2011-03-04  0:55:40
2011-05-16  0:55:40
2011-07-16  0:55:40


from datetime import datetime
from time import strptime

Now

datetime(*strptime('2011-03-08  0:27:41', '%Y-%m-%d  %H:%M:%S')[0:6]).weekday()

returns the day of the week for the first date "as an integer, where Monday is 0 and Sunday is 6", so selecting those dates for which weekday() in [0, 6] will give you the start and end dates of weeks (or use 4 instead of 6 for work weeks).


From python2.5 onwards datetime module has datetime.strptime(date_string, format) which you can pass the date_string and format to get a datetime object for doing comparisions. In python2.4, you could use the time module's support of strptime. From the documentation, you should be able to find the week part of your question.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜