本篇文章为大家展示了python中怎么返回特定的时间,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。
下方的 which_date() 函数实现返回某一起始时间后特定一段时间的日期。特定的一段时间有两种不同的格式:一种是不同的天数,如 "1 day" 或 "30 days",另一种是不同的星期数,如 "2 weeks" 或 "12 weeks"。
from datetime import datetime, timedelta
def which_date(start_date,time):
end_date=datetime.strptime(start_date,"%Y/%m/%d")
n=int(time.split()[0])
t=time.split()[1]
if t in 'days':
end_date=end_date+timedelta(days=n)
elif t in 'weeks':
end_date=end_date+timedelta(weeks=n)
end_date=end_date.strftime("%Y/%m/%d")
return end_date
def test():
assert which_date('2016/02/10','35 days') == '2016/03/16'
assert which_date('2016/12/21','3 weeks') == '2017/01/11'
assert which_date('2015/01/17','1 week') == '2015/01/24'
print("All tests completed.")
if __name__ == "__main__":
test()
上述内容就是python中怎么返回特定的时间,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注亿速云行业资讯频道。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。
原文链接:http://blog.itpub.net/25465866/viewspace-2147028/