- 方法一
from datetime import datetime, timedelta, timezone
datetime_zone = datetime.utcnow().replace(tzinfo=timezone.utc).astimezone(timezone(timedelta(hours=8)))
print(datetime_zone)
# 结果
# 2022-04-23 09:46:15.757988+08:00
- 方法二
from datetime import datetime, timedelta, timezone
expect_time = datetime.utcnow() + timedelta(hours=8)
# 结果
# 2022-04-23 09:30:28.274592
- 方法三
from datetime import datetime
import pytz
datetime_zone=datetime.utcnow().astimezone(pytz.timezone('Asia/Shanghai')) # 东八区
#拿到所有的时区
print(pytz.all_timezones)
使用arrow 包进行转化
import arrow
datetime_zone=arrow.get(arrow.utcnow(), tzinfo="+00:00").to("+08:00").format('YYYY-MM-DD HH:mm:ss')
print(datetime_zone)
# 结果
# 2022-04-23 09:58:27
date_time=arrow.get(arrow.utcnow(), tzinfo="+00:00").to("+08:00")
# 结果
# 2022-04-23 01:05:44.204117+08:00
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)