1 导入
import datetime, time
2 查看当前时间
datetime.datetime.now()
>> datetime.datetime(2021, 4, 20, 11, 16, 40, 342361)
3 自定义时间
a = datetime.datetime(2020,1,1)
>> datetime.datetime(2020, 1, 1, 0, 0)
4 时间转化为格式化日期
a = datetime.datetime(2020,1,1,1,1,1)
a.strftime('%Y-%m-%d %H:%M:%S')
>> '2020-01-01 01:01:01'
5 时间加减
a = datetime.datetime(2020,1,1,1,1,1)
b = datetime.timedelta(days=0, seconds=10, minutes=0, hours=0)
a+b
>> datetime.datetime(2020, 1, 1, 1, 1, 11)
(a+b).strftime('%Y-%m-%d %H:%M:%S')
>> '2020-01-01 01:01:11'
6 时间差
datetime.timedelta(hours=3,seconds=22).total_seconds()
>> 10822.0