给定一天,我想获取这一天所在的一周中的所有天(日期时间实例)。
我有一个解决方案,如果有更有效的方法有问题,请纠正我。
>>> import datetime
>>> today = datetime.datetime(2013, 06, 26)
>>> today
datetime.datetime(2013, 6, 26, 0, 0)
>>> day_of_week = today.isocalendar()[2] - 1
>>> day_of_week
2
>>> start_date = today - timedelta(days=day_of_week)
>>> start_date
datetime.datetime(2013, 6, 24, 0, 0) # Got monday
>>> dates = [start + timedelta(days=i) for i in range(7)]
>>> dates
[datetime.datetime(2013, 6, 24, 0, 0),
datetime.datetime(2013, 6, 25, 0, 0),
datetime.datetime(2013, 6, 26, 0, 0),
datetime.datetime(2013, 6, 27, 0, 0),
datetime.datetime(2013, 6, 28, 0, 0),
datetime.datetime(2013, 6, 29, 0, 0),
datetime.datetime(2013, 6, 30, 0, 0)]
我希望星期一是开始日期,星期天是结束日期。