时间函数

当地时间

import time
time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time()))
 

from datetime import datetime
time = datetime.now().strftime("%H:%M:%S")

时间运用

from datetime import datetime, timedelta

today = datetime.now()
print(today)
last_month_date = datetime(today.year, today.month, 1) - timedelta(days=1)
print(last_month_date)
dt = int(last_month_date.strftime('%Y%m%d'))
print(dt)
now = today.strftime('%Y%m%d')
print(now)

#结果
2023-04-18 09:48:09.015491
2023-03-31 00:00:00
20230331
20230418
posted @ 2021-04-15 16:26  半个程序猿Cohen_Lee  阅读(40)  评论(0编辑  收藏  举报