063 datatime模塊

datetime模塊

  • datetime模塊能夠當作是時間加減的模塊

1.import datetime

  • 導入datetime模塊
datetime.datetime.now()  # 打印當前時間
# 2019-08-16 19:55:21.141274


now = datetime.datetime.now()
print(now+datetime.timedelta(3))  # 默認+3天

print(now+datetime.timedelta(hours=3))  # 當天時間+3小時
print(now+datetime.timedelta(minutes=3))  # 當天時間+3分鐘


now.replace(year=1900)# 替換當前年份


print(datetime.date.fromtimestamp(time.time()))
# 2019-03-07


datetime.datetime.now() + datetime.timedelta(hours=3)# 當前時間-3小時
# 2019-03-07 19:22:14.575881

datetime.datetime.now() + datetime.timedelta(minutes=30)# 當前時間+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
相關文章
相關標籤/搜索