Python中的datetime模块主要是针对时间的格式和操作,一些常用的方法如下:
import datetime
print(datetime.datetime.now()) # 格式化当前的时间 2020-06-18 08:19:13.357307
print(datetime.datetime.fromtimestamp(1111111)) # 将一个时间戳转化为格式化的时间 1970-01-14 04:38:31
对时间进行加减
print(datetime.datetime.now()+datetime.timedelta(days=3)) # 2020-06-21 08:19:13.357307
print(datetime.datetime.now()+datetime.timedelta(hours=3)) # 2020-06-18 11:19:13.357307
print(datetime.datetime.now()+datetime.timedelta(minutes=5)) # 2020-06-18 08:24:13.357307
print(datetime.datetime.now()+datetime.timedelta(seconds=14)) # 2020-06-18 08:19:27.357307
替换时间
print(datetime.datetime.now().replace(year=2222)) # 2222-06-18 08:19:13.357307
print(datetime.datetime.now().replace(month=7)) # 2020-07-18 08:19:13.357307