Working with Datetime Objects and Timezones in Python
In this article, we are going to work with Datetime objects and learn about their behavior when Time zones are introduced. We are going to be working with the Python datetime module.
Getting a Datetime object
Method 1: Using now() method
A very easy way to get a Datetime object is to use the datetime.now() method. A DateTime object is an instance/object of the datetime.datetime class. The now() method returns an object that represents the current date and time.
Method 2: Defining the Datetime object manually
We can also declare the DateTime object manually
After this, we learn how to format our Datetime objects.
Formatting DateTime objects
Sometimes we need to print our datetime objects in different formats. For these operations, we are going to use strftime() method. The syntax of strftime() is:
Syntax : strftime(format)
Sun 12 01 12-09-2001 00:00:00:00AM
We can get different attributes of the datetime object. In the code below, we are going to get the hours, minutes, and seconds.
12 11 23
We can also get a datetime object using the strptime() method, in which we get a DateTime object from a string. Since the strings we use can be formatted in any form, we need to specify the expected format. To understand this, let’s look at the syntax of strptime():
Syntax : datetime.strptime(data,expected_format)
- data : Our time and date passed as a string in a particular format
- expected_format : the format in which data is presented in the first parameter
Working with Timezones
The DateTime objects that we have been working with till now are what are known as naive DateTime objects. A naive DateTime object does not have any information on the timezone. We can check this using the tzinfo property.
To set our own timezones, we have to start using the pytz module. In the next example, we will create a DateTime object first and then create a timezone object. We will then localize that timezone object to the DateTime object and check the tzinfo property.
Conversion of DateTime object from one Timezone to another
To convert the DateTime object from one timezone to another we need to use the astimezone() method.
Syntax : DateTimeObject.astimezone(tz=None)
tz : The specified timezone to which the DateTimeObject needs to be converted to
Returns : a datetime instance according to the specified time zone parameter tz
Working with the timedelta class
We can check the difference between two DateTime objects with are either both localized to the same timezone or are naive. In the next example, we are going to create two DateTime objects localized to the same timezone and check their difference. The difference of the time returned should be an object of the timedelta class.
-165 days, 0:49:47 164 days, 23:10:13 <class 'datetime.timedelta'>
Not just differences, timedelta objects can also be used for addition. If we add a timedelta object to a datetime object, we get another datetime object that has the timedelta factored in as the difference with the first datetime object. Let’s see the example :
Please Login to comment...