由字符串格式转化为日期格式的函数为: 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'>