date format with timezone

北城余情 提交于 2021-02-19 07:07:50

问题


How do I format a date in python to look like this: weekday:month:day(number):HH:MM:SS(military):EST/CST/PST:YYYY? I am familiar with strftime(), but I am unsure how I would handle the HH:MM:SS and EST/CST/PST.

example of how I am trying to get the date to look:

Sun Mar 10 15:53:00 EST 2013

回答1:


Use strftime to output a formatted string representation:

print time.strftime("%a %b %d %H:%M:%S %Z %Y")

A list of the format codes can be found here




回答2:


from time import gmtime, strftime
print strftime("%a %b %d %H:%M:%S %Z %Y", gmtime())

This will produce

Fri Mar 22 21:10:56 Eastern Standard Time 2013

You'll have to settle for the long name of the timezone unless you want to use pytz. I suppose it's worth noting that timezone abbreviations aren't unique.



来源:https://stackoverflow.com/questions/15579599/date-format-with-timezone

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