0

如何将带有时区信息的时间字符串转换为iso格式的字符串?

例如:

带有时区信息的时间字符串

time = "2012-01-01T10:30:00-05:00"

ISO 格式时间字符串

time = "2012-01-01T15:30:00Z"
4

1 回答 1

0
def timestamp(time):
    form dateutil import parser
    time = parser.parse(time) #Converts string in to datetime object 
    from pytz import UTC
    if time.tzinfo: #Checks if the time is aware or naive
        time = time.astimezone(UTC) #Converts aware time into UTC
    return time.strftime('%Y%m%dT%H%M%SZ')
于 2013-05-31T20:21:17.997 回答