datetime模块

一、datetime模块

1
2
# datetime模块可以看成是时间加减的模块
import datetime
1
2
# 返回当前时间
print(datetime.datetime.now())
1
2019-03-07 16:22:14.544130
1
print(datetime.date.fromtimestamp(time.time()))
1
2019-03-07
1
2
# 当前时间+3天
print(datetime.datetime.now() + datetime.timedelta(3))
1
2019-03-10 16:22:14.560599
1
2
# 当前时间-3天
print(datetime.datetime.now() + datetime.timedelta(-3))
1
2019-03-04 16:22:14.568473
1
2
# 当前时间-3小时
print(datetime.datetime.now() + datetime.timedelta(hours=3))
1
2019-03-07 19:22:14.575881
1
2
# 当前时间+30分钟
print(datetime.datetime.now() + datetime.timedelta(minutes=30))
1
2019-03-07 16:52:14.585432
1
2
3
# 时间替换
c_time = datetime.datetime.now()
print(c_time.replace(minute=20, hour=5, second=13))
1
2019-03-07 05:20:13.595493
posted @ 2021-05-07 19:12  silencio。  阅读(50)  评论(0编辑  收藏  举报