日期时间操作,用datetime模块就够了
1 from datetime import datetime 2 # current time 3 print(datetime.now()) #2022-09-02 04:31:09.492407 4 5 # init datetime 6 dt = datetime(2022, 9, 2, 12, 45, 11) 7 print(dt) #2022-09-02 12:45:11 8 9 # datetime to timestamp 10 dt = datetime.now() 11 print(dt.timestamp()) #1662093486.622928 12 13 # timestamp to datetime str 14 print(datetime.fromtimestamp(1662093486)) #2022-09-02 12:38:06 15 16 # str to datetime 17 dt = datetime.strptime('2022-09-02 12:38:06', '%Y-%m-%d %H:%M:%S') 18 print(dt) #2022-09-02 12:38:06 19 20 # datetime to str 21 dt = datetime.now() 22 print(dt.strftime('%Y-%m-%d %H:%M:%S')) 23 24 # datetime calculation 25 dt = datetime.strptime('2022-09-02 12:38:06', '%Y-%m-%d %H:%M:%S') 26 print(dt + timedelta(days=1, hours=1)) #2022-09-03 13:38:06
某个时区当前时间:
import pytz # 所有时区 print(pytz.all_timezones) # 指定时区 datetime.now(pytz.timezone('Hongkong')).strftime('%Y-%m-%d %H:%M:%S')
import datetime import time import pytz now = datetime.datetime.now(pytz.timezone('Hongkong')) # utc conext["st_time"] = int((time.mktime(base_time.timetuple()))) - 8*3600
python 2 转timestamp:
坑::用的是服务器本地时间,如果服务器设置的是utc....会把你坑死
int((time.mktime(base_time.timetuple()) + base_time.microsecond/1000000.0))
---
开心工作,认真生活;回望来时路,脚印三两,笑声无数...
开心工作,认真生活;回望来时路,脚印三两,笑声无数...