time() 打印出时间戳
strftime 将时间格式化输出
strptime 将字符串按格式转化为时间
__author__ = 'Bao'
import time
print(time.time())
t1=time.localtime()
print(time.strftime('%Y-%m-%d %H:%M:%S',t1))
t=time.strptime('2021-05-09 20:46:20','%Y-%m-%d %H:%M:%S')
print(t)
输出结果:
1620564447.982564
2021-05-09 20:47:27
time.struct_time(tm_year=2021, tm_mon=5, tm_mday=9, tm_hour=20, tm_min=46, tm_sec=20, tm_wday=6, tm_yday=129, tm_isdst=-1)
datetime.datetime.now() 当然日期时间
__author__ = 'Bao'
import datetime
print(datetime.datetime.now())
输出结果:
2021-05-09 21:02:03.759811
datetime.timedelta() 某个日期前后
默认是日,days=0, seconds=0, microseconds=0, milliseconds=0, minutes=0, hours=0, weeks=0
#-*- coding: utf-8 -*-
__author__ = 'Bao'
import datetime
print(datetime.datetime.now())
print(datetime.datetime.now()+datetime.timedelta(2)) #两天后
print(datetime.datetime.now()+datetime.timedelta(-2)) #两天前
print(datetime.datetime.now()+datetime.timedelta(hours=2)) #两小时后
输出结果:
2021-05-09 21:10:39.993157
2021-05-11 21:10:39.993157
2021-05-07 21:10:39.993157
2021-05-09 23:10:39.993157