[Python]Python日期格式和字符串格式相互转换

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

2020-02-18 10:54:45

你可能感兴趣的:([Python]Python日期格式和字符串格式相互转换)