1、utc与本地时间互转
# python3.6 from datetime import datetime from pytz import utc # 本地时间 dt_loc = datetime.now() # 本地时间转UTC时间 dt_utc = dt_loc.astimezone(utc) # UTC时间转本地时间 dt_loc1 = dt_utc.astimezone() # 字符串为UTC时间,需要调整到LOC时间,差值通过计算得到 utc_dt = datetime.strptime("2021-11-05 08:00:00", "%Y-%m-%d %H:%M:%S") now_stamp = time.time() loc = datetime.fromtimestamp(now_stamp) utc = datetime.utcfromtimestamp(now_stamp) utc_offset = loc - utc loc_dt = utc_offset + utc_dt
2、日期与字符串互转
from datetime import datetime # 字符串转时间 dt = datetime.strptime("2021-11-05 08:00:00", "%Y-%m-%d %H:%M:%S") # 时间转字符串 dt_str = dt.strftime("%Y-%m-%d %H:%M:%S")
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)