python 将字符串时间转换为date对象

import datetime
today = "2021-05-08"
print(today)
print(type(today))
nd = datetime.date(*map(int, today.split('-')))
print(nd, type(nd))
#得到nd时间对象后我们就可以方便的求其前几天或者后几天的时间了
tmo = nd + datetime.timedelta(days=1)    #获取后一天
tmo2 = nd + datetime.timedelta(days=-1)  #获取前一天
print('tmo,', tmo)

#以上代码输出:
2021-05-08
<class 'str'>
2021-05-08 <class 'datetime.date'>
tmo, 2021-05-09


猜你喜欢

转载自blog.csdn.net/sasibingdu/article/details/118571661