比如把格林威治时间转换为上海时间:
from datetime import datetime import pytz print('格林威治时间: ', datetime.now().strftime("%Y-%m-%d_%H-%M-%S")) print('上海时间: ', datetime.now().astimezone(pytz.timezone("Asia/Shanghai")).strftime("%Y-%m-%d_%H-%M-%S"))
输出:
这两个时区相差八小时。
比如把格林威治时间转换为上海时间:
from datetime import datetime import pytz print('格林威治时间: ', datetime.now().strftime("%Y-%m-%d_%H-%M-%S")) print('上海时间: ', datetime.now().astimezone(pytz.timezone("Asia/Shanghai")).strftime("%Y-%m-%d_%H-%M-%S"))
输出:
这两个时区相差八小时。