获取今天的日期,t-1、t-2、t+1的日期:
import datetime
now = datetime.datetime.now()
day = datetime.timedelta(days=1)
now = (now - 4 * day)
t0 = now.strftime('%Y%m%d')
t1 = (now - 1 * day).strftime('%Y%m%d')
t2 = (now - 2 * day).strftime('%Y%m%d')
t_1 = (now + 1 * day).strftime('%Y%m%d')
print(t1)
计算两个日期字符串之间的间隔天数:
dt = '20220101'
t0 = '20220106'
dt_format = datetime.datetime.strptime(dt, '%Y%m%d').date()
t0_format = datetime.datetime.strptime(t0, '%Y%m%d').date()
days = (t0_format - dt_format) / day