Justin-刘清政的博客

python/常用模块/2-datetime模块

2020-12-21

一、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
使用支付宝打赏
使用微信打赏

点击上方按钮,请我喝杯咖啡!

扫描二维码,分享此文章