convert gmt to mountain time in python

To convert GMT to Mountain Time in Python, you can use the datetime and timezone modules. Here's an example code snippet:

main.py
import datetime
import pytz

gmt_time = datetime.datetime.now(pytz.timezone('GMT'))

mountain_time = gmt_time.astimezone(pytz.timezone('US/Mountain'))

print("GMT Time: ", gmt_time.strftime("%Y-%m-%d %H:%M:%S %Z%z"))
print("Mountain Time: ", mountain_time.strftime("%Y-%m-%d %H:%M:%S %Z%z"))
292 chars
10 lines

In the above code, we first use the datetime.datetime.now() function to get the current datetime in GMT timezone. We then use the astimezone() method to convert this datetime to Mountain Time (US/Mountain timezone). Finally, we print the converted datetime values in both GMT and Mountain Time formats using the strftime() method.

Note: Make sure to install the pytz module that provides the Olson time zone database for Python to work with timezone conversions as shown in the example above.

gistlibby LogSnag