我希望根据周号获取本周的所有日期。
我可以得到像
import datetime
weeknum=datetime.datetime.now().isocalendar()[1]
结果是20。
想要第20周的所有日期,最终输出应为
dates=['2019-05-12','2019-05-13','2019-05-14','2019-05-15','2019-05-16','2019-05-17','2019-05-18']
请帮我。
您可以使用time
,datetime
以及此答案的建议,但可以将其更改为您的要求:
WEEK = 20 - 2 # as it starts with 0 and you want week to start from sunday
startdate = time.asctime(time.strptime('2019 %d 0' % WEEK, '%Y %W %w'))
startdate = datetime.datetime.strptime(startdate, '%a %b %d %H:%M:%S %Y')
dates = [startdate.strftime('%Y-%m-%d')]
for i in range(1, 7):
day = startdate + datetime.timedelta(days=i)
dates.append(day.strftime('%Y-%m-%d'))
输出如下:
dates = ['2019-05-12',
'2019-05-13',
'2019-05-14',
'2019-05-15',
'2019-05-16',
'2019-05-17',
'2019-05-18']
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句