Python Timezone.md

无论当前服务器设置如何,输出Asia/Shanghai的时间。

import datetime
import pytz

utc_now = datetime.datetime.utcnow() # Get current time in UTC
utc_timezone = pytz.utc
utc_now = utc_timezone.localize(utc_now) # Localize the time as UTC

# Convert to another timezone, e.g., 'America/New_York'
new_timezone = pytz.timezone('Asia/Shanghai')
new_timezone_time = utc_now.astimezone(new_timezone)

print(new_timezone_time.strftime('%Y-%m-%d %H:%M:%S %Z%z')) # Display time in the new timezone
til  python 

也可以看看