Getting Time Zone–Aware Python Time and Date Objects
If you want to add time zone awareness to your object, you can use the
.astimezone() method. In order to test this out, you can create a new variable called
now_aware and set it equal to your existing
now, followed by the
.astimezone() method, This code creates a new
datetime object called
now_aware, which has awareness of your current time zone.
As you can see, the code returns
BST, showing that my time zone is British Standard Time. The results of this code will differ depending on where you live in the world and what time zone you’re in.
Depending on what sort of project you’re working on, you may want to normalize your timestamps to UTC time. This is especially important if you’re working on a project in a sector which generally works off UTC time as a standard, such as in aviation or meteorology. To normalize timestamps, you can work with both the
datetime module as well as the
02:59 The next video will conclude the series summarizing what you have learned across the previous lessons. I’ll also share some additional resources that may be helpful for you to learn more about working with time in Python.
Become a Member to join the conversation.