w3resource

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

Arrow Module: Exercise-9 with Solution

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.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://w3resource.com/python-exercises/arrow/python-arrow-exercise-9.php