convert australian eastern time to gmt in python

To convert Australian Eastern Time (AET) to Greenwich Mean Time (GMT), you can use the datetime module in Python. First, you need to create a datetime object with the AET timezone, and then convert it to the GMT timezone.

Here's an example code snippet:

main.py
from datetime import datetime
from pytz import timezone

# create a datetime object with AET timezone
aet = timezone('Australia/Sydney')
dt_aet = datetime(2021, 10, 5, 14, 30, tzinfo=aet)

# convert to GMT timezone
gmt = timezone('GMT')
dt_gmt = dt_aet.astimezone(gmt)

print(dt_aet)  # 2021-10-05 14:30:00+10:00
print(dt_gmt)  # 2021-10-05 04:30:00+00:00
356 chars
14 lines

In this example, we create a datetime object dt_aet with the AET timezone, using the pytz.timezone() function. We then create a timezone object for the GMT timezone, and use the astimezone() method to convert dt_aet to dt_gmt. Finally, we print both datetime objects to verify that the conversion worked correctly.

gistlibby LogSnag