convert hawaii time to gmt in python

You can convert Hawaii time to GMT using the datetime module and timezone class in Python. Here's an example code snippet that shows how to convert a Hawaii time string to GMT:

main.py
import datetime
from pytz import timezone

# create Hawaii timezone object
hawaii_tz = timezone('US/Hawaii')

# create datetime object from Hawaii time string
hawaii_time_str = '2021-09-01 10:30:00'
hawaii_time = datetime.datetime.strptime(hawaii_time_str, '%Y-%m-%d %H:%M:%S')

# convert Hawaii time to GMT
gmt_tz = timezone('GMT')
gmt_time = hawaii_tz.normalize(hawaii_time.astimezone(gmt_tz))

# print GMT time as string
gmt_time_str = gmt_time.strftime('%Y-%m-%d %H:%M:%S')
print("GMT time:", gmt_time_str)
511 chars
18 lines

In this example, we first create a timezone object for Hawaii using pytz.timezone('US/Hawaii'). We then create a datetime object from the Hawaii time string using datetime.datetime.strptime().

To convert the Hawaii time to GMT, we create another timezone object for GMT, and then use the astimezone() method to convert the Hawaii time to GMT. Finally, we use strftime() to format the GMT time as a string and print it out.

gistlibby LogSnag