time
时间戳转化时间格式
dt = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(time.time()))
时间格式转时间戳
timestamp = int(time.mktime(time.strptime(dt, "%Y-%m-%d %H:%M:%S")))
datetime
时区
sha_tz = datetime.timezone(
datetime.timedelta(hours=8),
name='Asia/Shanghai',
)
timestamp = datetime.datetime.now().timestamp()
datetime_obj = datetime.datetime.utcfromtimestamp(timestamp).replace(tzinfo=timezone.utc).astimezone(sha_tz)
datetime_obj.strftime("%Y-%m-%d %H:%M:%S")
时间戳转化时间格式
datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
timestamp = datetime.datetime.now().timestamp()
datetime.datetime.fromtimestamp(timestamp).strftime("%Y-%m-%d %H:%M:%S")
timestamp = datetime.datetime.now().timestamp()
datetime.datetime.utcfromtimestamp(timestamp).strftime("%Y-%m-%d %H:%M:%S")
时间格式转时间戳
datetime_obj = datetime.datetime.strptime("2022-01-01 00:00:00", "%Y-%m-%d %H:%M:%S")
datetime_obj.timestamp()
时间单位 |
符号 |
周 |
%a |
月 |
%b |
日 |
%d |
时 |
%H |
分 |
%M |
秒 |
%S |
年 |
%Y |
时区 |
%z |
示例
- "2022-01-01 00:00:00"
- "%Y-%m-%d %H:%M:%S"
- "Fri Oct 14 09:01:06 +0000 2022"
- "%a %b %d %H:%M:%S %z %Y"