datetime模块strftime和strptime的区别

strptime():p表示parse,解析的意思,传入字符串和分析格式,返回一个时间对象
strftime():f表示format,表格式化,传入时间对象和分析格式,返回一个字符串
参考代码:

import datetime
time1=datetime.datetime(2020, 7, 21, 22, 31, 34)
print(type(time1),time1)
time2=datetime.datetime.strftime(time1,"%Y-%m-%d %H:%M:%S")
print(type(time2),time2)
time3=datetime.datetime.strptime('20200721 14:10:20',"%Y%m%d %H:%M:%S")
print(type(time3),time3)

返回结果:

<class 'datetime.datetime'> 2020-07-21 22:31:34
<class 'str'> 2020-07-21 22:31:34
<class 'datetime.datetime'> 2020-07-21 14:10:20

你可能感兴趣的:(python,python)