字符串转换为日期类型(<class 'datetime.datetime'>),并且做计算

来源:互联网 发布:天津mac口红专柜 编辑:程序博客网 时间:2024/06/07 07:09

两年前做PHP时候表单传输的东西现在都忘了,学习Django的时候从头搞起来昨晚搞到两点多,今早上类型转换搞的头大,闲语少叙,今早想做日起计算,表单传过来的取完是字符串类型,想转日期类型利用timedelta做计算,Google半天发现cookbook有介绍datetime标准库一下就能解决了,柳暗花明又一村的感觉啊,家里买了一本一直没看,抽空得看下啦,另外不熟悉标准库有坑啊,搞完这个要补下

从from提交的表单里获取字符串转换为日期,并且做计算

#我是字符串startdateformetstr = request.POST['startdate']#转为datetime.datetime类型print(type(datetime.strptime(startdateformetstr, "%Y/%m/%d")))print((datetime.strptime(startdateformetstr, "%Y/%m/%d")))#转完了就能和timedelta一起玩耍啦print((datetime.strptime(startdateformetstr, "%Y/%m/%d"))+ timedelta(1))

在线阅读:http://python3-cookbook.readthedocs.io/zh_CN/latest/c03/p15_convert_strings_into_datetimes.html

阅读全文
0 0
原创粉丝点击