Add Hours to datetime in Python

This tutorial discusses how to add Hours to datetime in Python.

datetime in Python

To work with date and time values, we can use the datetime module provided in Python. With this module, we can create datetime objects that have different attributes for storing the day, month, year, hour, minutes, seconds, and milliseconds. We can manipulate and modify such objects.

Ways to add hours to datetime in Python

Different methods on how to add hours to datetime in Python are discussed below.

Using the datetime.Hour Attribute to Add Hours to datetime in Python

The datetime constructor is used to create a datetime object in Python. The different attributes for date and time can be specified with this constructor. These objects are not writeable.

There are the hour, minute, second, and microsecond attributes for the time values.

The hour attribute can be used to add hours to datetime in Python.

For example,

Output:

2022-06-10 16:20:15
16

Using the datetime.timedelta object to add hours to datetime in Python

Directly, we cannot either add or subtract datetime objects in Python. There is another object that acts as an intermediate for such cases.

The datetime.timedelta object represents some type of difference between datetime objects. Such objects can be used to add hours to datetime in Python.

First, we need to create a datetime.timedelta object using the timedelta constructor. We will assign this object with the required hours we want to add using the hours attribute. This object will be then added to a datetime object.

See the code below.

Output:

2022-05-08 17:20:15

Using the dateutil.Relativedelta Object to Add Hours to datetime in Python

In Python, we can use the dateutil module as an extension to the datetime module by providing more functionalities that can be used with datetime objects.

The dateutil.relativedelta object is similar to the previously discussed timedelta object. It is used to represent some time intervals. The main difference lies in the parameters between the two objects. The relativedelta object accepts a wide range of parameters for date values, something which is not present in the timedelta object.

The relativedelta object can be used to add hours to datetime in Python.

See the following example.

Output:

2022-06-10 18:20:15

In the above example, we create a relativedelta object and specify the hours we want to add using the hours attribute. This object is added to a given datetime object.

Using the pandas.Date Offset() Function to Add Hours to datetime in Python

The pandas module is used to create and work with DataFrame objects that represent data in rows and columns. Many times these DataFrames encounter date and time values.

That is why the module is equipped with functionalities to manipulate and work with such values. The DateOffset() function adds hours to datetime in Python.

This function is used to add time intervals to given values. We can use the hours attribute to specify the number of hours to be added and use it with the datetime object.

For example,

Output:

2022-05-08 17:20:15

Using the arrow.shift() function to add hours to arrow object in Python

The arrow module is a new third-party module introduced recently and offers a relative ease and efficiency for working with date and time values. The arrow object can also store the date and time values.

The shift() function of this module can be used to update the values of a given arrow object. We can add or remove different attributes like hours, seconds, days, months, and more.

We can use this function with the hours parameter to add hours to an object in Python.

For example,

Output:

2022-05-07T04:25:27.832145+05:30

Conclusion

In this article, we discussed several methods to add hours to datetime in Python. We started with the basic use of the datetime module and the timedelta object. We then proceeded to discuss methods from other modules that can be used. The dateutil.relativedelta and pandas.Offset() are the two methods from other modules that can add hours to datetime in Python. We also discussed a different module called arrow and how to add hours to objects of this module.

That’s all about how to add Hours to datetime in Python.

Was this post helpful?

Leave a Reply

Your email address will not be published. Required fields are marked *