I have the time string like this "2023-09-06T22:02:44-07:00", which is obtained through calling
datetime.now().astimezone().isoformat(timespec='seconds')
My question is how to convert the time string "2023-09-06T22:02:44-07:00" to utc time? Basically how to convert it so that -07:00 will be +00:00?
You can first use the
fromisoformatmethod to convert the timestamp string to adatetimeobject, then call theastimezone(timezone.utc)method to convert it to UTC timezone, and output the converted timestamp in ISO format withisoformat:This outputs: