You can use the pytz
library to convert a datetime from one timezone to another. Here's an example of how to convert Central Time (US & Canada) to GMT:
main.py335 chars13 lines
In this example, datetime.datetime.now(pytz.timezone('US/Central'))
creates a new datetime object with the current date and time in the US Central Timezone. Then, we convert it to GMT timezone using ct.astimezone(gmt)
and store the result in gmt_time
. Finally, we print both datetimes with their respective timezone codes.
Note that you need to install pytz
library before using it. You can install it via pip by running $ pip install pytz
in your terminal.
gistlibby LogSnag