当前,我正在尝试将季节性运行时间表添加到我拥有的当前程序中。我想出了下面的代码可以正常工作,但是我想这样做,而不必在定义的日期中定期更新年份。
from datetime import date
td = date.today()
fs = date(2018, 3, 31)
fe = date(2018, 10, 18)
ws = date(2018, 5, 31)
we = date(2019, 3, 18)
sps = date(2018, 10, 1)
spe = date(2019, 5, 18)
sus = date(2018, 11, 1)
sue = date(2019, 9, 17)
if fs < td < fe:
print "FALL"
if ws < td < we:
print "WINTER"
if sps < td < spe:
print "SPRING"
if sus < td < sue:
print "SUMMER"
使用当前代码,如果今天是18/10/5,则显示:
FALL
WINTER
SPRING
答案 0 :(得分:0)
代码似乎与年份无关(或者我是否误解了您的代码?),所以也许您可以将当前年份作为基准。
from datetime import date
td = date.today()
current_year = td.year
fa_s = date(current_year , 3, 31)
fa_e = date(current_year , 10, 18)
wi_s = date(current_year , 5, 31)
wi_e = date(current_year + 1, 3, 18)
sp_s = date(current_year , 10, 1)
sp_e = date(current_year + 1, 5, 18)
su_s = date(current_year , 11, 1)
su_e = date(current_year + 1, 9, 17)