时间:2021-07-01 10:21:17 帮助过:121人阅读
from datetime import datetime
text = '2012-09-20'
y = datetime.strptime(text, '%Y-%m-%d')
print(y)
z = datetime.now()
diff = z - y
print(diff)
按特定格式输出
nice_z = datetime.strftime(z, '%A %B %d, %Y')
print(nice_z)
datetime.strftime性能很差,下面自己编写一个函数
from datetime import datetime
def parse_ymd(s):
year_s, mon_s, day_s = s.split('-')
return datetime(int(year_s), int(mon_s), int(day_s))