w3resource

Python Arrow Module: Convert timezone from local to utc, utc to local or specified zones


Write a Python program to convert timezone from local to utc, utc to local or specified zones.

Sample Solution:

Python Code:

import arrow
utc = arrow.utcnow()
print("utc:")
print(utc)
print("\nutc to local:")
print(utc.to('local'))
print("\nlocal to utc:")
print(utc.to('local').to('utc'))
print("\nutc to specific location:")
print(utc.to('US/Pacific'))

Sample Output:

utc:
2019-06-01T08:59:27.089800+00:00

utc to local:
2019-06-01T14:29:27.089800+05:30

local to utc:
2019-06-01T08:59:27.089800+00:00

utc to specific location:
2019-06-01T01:59:27.089800-07:00

Python Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a Python program to replace hour, minute, day, month, year and timezone with specified value of current datetime using arrow.
Next: Write a Python program to create a string representation of the Arrow object, formatted according to a format string.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.