How to convert string timezones in form (Country/city) into datetime.tzinfo

前端 未结 1 983
天命终不由人
天命终不由人 2020-12-18 22:55

Is there a built-in library or does anyone have available a function to convert a string timezone such as \"America/New_York\" to a datetime.tzinfo object?

Thanks.

相关标签:
1条回答
  • 2020-12-18 23:31

    Yes, you need the pytz library:

    import datetime, pytz
    zoneName = 'America/New_York'
    now = datetime.datetime.now(pytz.timezone(zoneName))
    

    returns:

    datetime.datetime(2011, 3, 16, 1, 39, 33, 87375, tzinfo=<DstTzInfo 'America/New_York' EDT-1 day, 20:00:00 DST>)
    
    0 讨论(0)
提交回复
热议问题