python 自定义时间 加减天数


import datetime



# 现在的时间
setTime ='2015-04-16'
now=datetime.datetime.strptime(setTime,'%Y-%m-%d')
print(now)
# 递增的时间
delta = datetime.timedelta(days=1)
# 88天后的时间
endnow = now + datetime.timedelta(days=88)#设置小时的就改成hours
# 六天后的时间转换成字符串
endnow = str(endnow.strftime('%Y-%m-%d'))
offset = now
# 当日期增加到88天后的日期,循环结束
while str(offset.strftime('%Y-%m-%d')) != endnow:
    offset += delta
    print(str(offset.strftime('%Y-%m-%d')))

结果

2015-04-16 00:00:00
2015-04-17
2015-04-18
2015-04-19
2015-04-20
2015-04-21
2015-04-22
2015-04-23
2015-04-24
2015-04-25
2015-04-26
2015-04-27
2015-04-28
2015-04-29
2015-04-30
2015-05-01
2015-05-02
2015-05-03
2015-05-04
2015-05-05
2015-05-06
2015-05-07
2015-05-08
2015-05-09
2015-05-10
2015-05-11
2015-05-12
2015-05-13
2015-05-14
2015-05-15
2015-05-16
2015-05-17
2015-05-18
2015-05-19
2015-05-20
2015-05-21
2015-05-22
2015-05-23
2015-05-24
2015-05-25
2015-05-26
2015-05-27
2015-05-28
2015-05-29
2015-05-30
2015-05-31
2015-06-01
2015-06-02
2015-06-03
2015-06-04
2015-06-05
2015-06-06
2015-06-07
2015-06-08
2015-06-09
2015-06-10
2015-06-11
2015-06-12
2015-06-13
2015-06-14
2015-06-15
2015-06-16
2015-06-17
2015-06-18
2015-06-19
2015-06-20
2015-06-21
2015-06-22
2015-06-23
2015-06-24
2015-06-25
2015-06-26
2015-06-27
2015-06-28
2015-06-29
2015-06-30
2015-07-01
2015-07-02
2015-07-03
2015-07-04
2015-07-05
2015-07-06
2015-07-07
2015-07-08
2015-07-09
2015-07-10
2015-07-11
2015-07-12
2015-07-13

Process finished with exit code 0

 

# 现在的时间
starttime=1555406042
input="昨天 13:48"
setTime=str(time.strftime("%Y-%m-%d", time.localtime(starttime)))
print(setTime)
now=datetime.datetime.strptime(setTime,"%Y-%m-%d")


endnow =str (now - datetime.timedelta(days=1))#设置小时的就改成hours
creatTime = (input.strip("昨天 "))+":00"

endnow=endnow[0:11]+creatTime
print(endnow)

随便给定的时间修改格式

你可能感兴趣的:(python3)