在Python中根据当前日期显示日期范围 [英] Display a range of dates based on current date, in Python
本文介绍了在Python中根据当前日期显示日期范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
import datetime
print datetime.datetime.now().strftime("Week of %m/%d")
#returns "Week of 04/18"
我希望它打印 4/11周至4/18周(其中4/13恰好是一周前的一周)
,并且如果该周结束于4/3,则需要说明这一点,那么它将是 3/27到4/3的一周
I want it to print "Week of 4/11 to 4/18" (with 4/13 being exactly one week beforehand) and it would need to account for if the week ended on 4/3, then it would be "Week of 3/27 to 4/3"
有一种简单的方法吗?
推荐答案
dateutil.relativedelta
会做您想要做的:
dateutil.relativedelta
will do what you want:
import datetime
from dateutil.relativedelta import *
march27 = datetime.datetime(2014, 3, 27)
print (march27 + relativedelta(weeks=+1)).strftime("Week of %m/%d")
#prints "Week of 04/03"
这篇关于在Python中根据当前日期显示日期范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文