常用时间操作的函数汇总, 涵盖 常用的time datetime
1.计算两个日期相差天数
import datetime str1 = '2021-10-20' str2 = '2021-10-22' date1 = datetime.datetime.strptime(str1[0:10], '%Y-%m-%d') date2 = datetime.datetime.strptime(str2[0:10], '%Y-%m-%d') days = (date2-date1).days
2.获取日期的开始与结束时间
import datetime today = datetime.date.today() min_today_time = datetime.combine(today, time.min) max_today_time = datetime.combine(today, time.max) # 昨天的日期 yesterday = today - datetime.timedelta(days=1) ...