Python日期格式和字串格式相互轉換的方法
阿新 • • 發佈:2020-02-19
由字串格式轉化為日期格式的函式為: datetime.datetime.strptime()
由日期格式轉化為字串格式的函式為: datetime.datetime.strftime()
# encoding: utf-8 import datetime day = datetime.datetime.strptime('2020-2-18 10:54:45','%Y-%m-%d %H:%M:%S') print(day) print type(day) day = datetime.datetime.strftime(day,'%Y-%m-%d %H:%M:%S') print(day) print type(day)
root@tao:/var/www/html/python# python 3.py
2020-02-18 10:54:45
<type 'datetime.datetime'>
2020-02-18 10:54:45
<type 'str'>
總結
以上所述是小編給大家介紹的Python日期格式和字串格式相互轉換的方法,希望對大家有所幫助