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

那年仲夏 提交于 2019-12-19 05:24:18

问题


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:


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>)


来源:https://stackoverflow.com/questions/5321466/how-to-convert-string-timezones-in-form-country-city-into-datetime-tzinfo

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!