使用的第三方库
datetime、time
代码实现
import time
import datetime
now_time = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime())
print(f'打印今天的日期时间:{now_time}')
now_time = time.strftime('%Y-%m-%d', time.localtime())
print(f'打印今天的日期:{now_time}')
today = datetime.date.today()
print(f'打印今天的日期:{now_time}')
today = datetime.date.today()
yesterday = today - datetime.timedelta(days=1)
print(f'打印昨天的日期:{yesterday}')
today = datetime.date.today()
tomorrow = today + datetime.timedelta(days=1)
print(f'打印明天的日期:{tomorrow}')
today = datetime.date.today()
mb_day = today - datetime.timedelta(days=60)
print(f'打印从今天往前算两个月的时间:{mb_day}')
运行结果
打印今天的日期时间:2023-01-06 17:46:30
打印今天的日期:2023-01-06
打印今天的日期:2023-01-06
打印昨天的日期:2023-01-05
打印明天的日期:2023-01-07
打印从今天往前算两个月的时间:2022-11-07