Skip to content
Advertisement

How to convert timezone in number to UTC?

I have some random dates with different timezones, they are in formats like this "07 Mar 2022 13:52:00 -0300", or they could be like this: "07 Mar 2022 11:12:00 -0700". I don’t know which timezone exactly they will be coming from. How can I convert all of them to UTC time "0000Z"?

Advertisement

Answer

You can use standard module datetime for this.

Function strptime() (string parsing time) can convert string to object datetime using matching pattern. For your examples works pattern '%d %b %Y %H:%M:%S %z'

Next you can use .astimezone(datetime.timezone.utc) to convert to UTC.

And later you can format string with strftime() (string formatting time) using again pattern '%d %b %Y %H:%M:%S %z' (or you can skip %z)


Minimal working code:

JavaScript

Result:

JavaScript
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement