python转换字符串为datetime类型

2015-03-24 0 780
python转换字符串为datetime类型

python中通过日期转换函数把字符串格式的日期转换成datetime格式的日期格式

time.strptime() converts the string to a struct_time tuple.

time.mktime() converts this tuple into seconds (elasped since epoch, C-style).

datetime.fromtimestamp() converts the seconds to a Python datetime object.

>>> import datetime,time
>>> stringDate = "2006-05-18 19:35:00"
>>> dt = datetime.datetime.fromtimestamp(time.mktime(time.strptime(stringDate,"%Y-%m-%d %H:%M:%S")))
>>> print dt
2006-05-18 19:35:00
>>> print type(dt)
<type 'datetime.datetime'>
>>>

遇见资源网 python python转换字符串为datetime类型 http://www.ox520.com/15167.html

常见问题

相关文章

发表评论
暂无评论
官方客服团队

为您解决烦忧 - 24小时在线 专业服务