python轉換字符串為datetime類型

g2b4 9年前發布 | 3K 次閱讀 Python

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'>
>>>

 本文由用戶 g2b4 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!