To convert GMT to New Zealand Time in Python, use the pytz library and its timezone functionality. Here's an example code snippet:
main.py431 chars14 lines
In this example, we first create a datetime object for a specific GMT time by specifying the year, month, day, hour, minute, and second, and including the tzinfo
parameter to indicate the timezone as pytz.timezone('GMT')
.
We then use the astimezone()
method to convert this GMT time to the New Zealand timezone, specified as pytz.timezone('Pacific/Auckland')
.
Finally, we format the New Zealand time as a string using the strftime()
method with the desired format ('%Y-%m-%d %H:%M:%S %Z%z'
), and print the result.
Note that the strftime()
method also allows for various other formatting options, such as displaying the time in a different time zone or using a different date/time format.
gistlibby LogSnag