datetime模块

2023-01-31 00:01:31 datetime 模块

datetime模块一般用于时间的加减。

# datetime模块可以看成是时间加减的模块
import datetime
# 返回当前时间
print(datetime.datetime.now())
2019-03-07 16:22:14.544130
print(datetime.date.fromtimestamp(time.time()))
2019-03-07
# 当前时间+3天
print(datetime.datetime.now() + datetime.timedelta(3))
2019-03-10 16:22:14.560599
# 当前时间-3天
print(datetime.datetime.now() + datetime.timedelta(-3))
2019-03-04 16:22:14.568473
# 当前时间-3小时
print(datetime.datetime.now() + datetime.timedelta(hours=3))
2019-03-07 19:22:14.575881
# 当前时间+30分钟
print(datetime.datetime.now() + datetime.timedelta(minutes=30))
2019-03-07 16:52:14.585432
# 时间替换
c_time = datetime.datetime.now()
print(c_time.replace(minute=20, hour=5, second=13))
2019-03-07 05:20:13.595493

相关文章