To convert a datetime in Mountain Time to GMT (also known as UTC), you can use the pytz library. Here's an example code snippet:
main.py311 chars13 lines
In this code, we first create a datetime object representing the Mountain Time we want to convert (in this case, October 20, 2021 at 4:30 PM Mountain Time).
Then, we create a timezone object for Mountain Time with pytz.timezone('US/Mountain')
. We use this timezone object as the tzinfo
attribute when creating the datetime object.
Next, we use the astimezone()
method to convert the datetime object to Greenwich Mean Time (GMT) or Coordinated Universal Time (UTC). We pass in pytz.utc
as the timezone object to convert to.
Finally, we print out the original Mountain Time datetime object as well as the converted GMT/UTC datetime object.
gistlibby LogSnag