【发布时间】:2016-12-15 21:08:56
【问题描述】:
我正在尝试构建一个接收日期并添加天数的函数,更新所有内容以防万一它发生变化,到目前为止我已经想出了这个:
def addnewDate(date, numberOfDays):
date = date.split(":")
day = int(date[0])
month = int(date[1])
year = int(date[2])
new_days = 0
l = 0
l1 = 28
l2 = 30
l3 = 31
#l's are the accordingly days of the month
while numberOfDays > l:
numberOfDays = numberOfDays - l
if month != 12:
month += 1
else:
month = 1
year += 1
if month in [1, 3, 5, 7, 8, 10, 12]:
l = l3
elif month in [4, 6, 9, 11]:
l = l2
else:
l = l1
return str(day) + ':' + str(month) + ':' + str(year) #i'll deal
#with fact that it doesn't put the 0's in the < 10 digits later
期望的输出:
addnewDate('29:12:2016', 5):
'03:01:2017'
我认为问题出在变量上,或者我使用它们的位置,虽然有点迷失了..
提前致谢!
p.s 我不能使用 python 内置函数 :)
【问题讨论】:
-
有时
l1 = 29(每 4 年一次,但不能被 400 整除) -
哦,对了,忘记了,但为了简单起见,我们假设它只到 28
标签: python time while-loop