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?


import_contacts

You may also like:

Related Posts

  • 26 December

    Get First Day of Next Month in Python

    Table of ContentsGet First Day of Next Month in PythonUsing the datetime.replace() with datetime.timedelta() functionUsing the calendar.monthrange() functionUsing the dateutil.relativedelta objectConclusion Get First Day of Next Month in Python This tutorial will demonstrate how to get first day of next month in Python. Using the datetime.replace() with datetime.timedelta() function To get first day of next […]

  • 07 December

    Get List of Months Between Two Dates in Python

    Table of ContentsUse pandas.period_range() MethodUse Nested List Comprehension Use pandas.period_range() Method To get the list of months between two specified dates in Python: Use pandas’s period_range() to get list of months between start date and end date with freq='M'. Use list comprehension to iterate over list of months. For each month, use strftime() method to […]

  • 28 November

    Check if Date is Greater than Today in Python

    Table of ContentsUse Comparison Operator with now()Compare the datetime of Different Time ZonesUse Comparison Operator with strptime() Use Comparison Operator with now() To check if the specified date is greater than today in Python: Use .datetime.now() to get the current local time and date. Create a datetime object using datetime.datetime() with the specified date and […]

  • 26 October

    Get Number of Business Days Between Two Dates in Python

    Table of ContentsGet Number of Business Days Between Two Dates in PythonUsing the weekday() FunctionUsing the numpy.busday_count() FunctionUsing the workdays LibraryConclusion Python provides the datetime object to store and work with date and time values. These objects are compatible with various libraries and can be manipulated using different functions from such libraries. One such operation […]

  • 25 October

    Get All Dates Between Two Days in Python

    Table of ContentsGet All Dates Between Two Days in PythonUsing the datetime.timedelta ObjectUsing the pandas.date_range() FunctionUsing the dateutil LibraryUsing the numpy.arange() FunctionConclusion In Python, we can efficiently work with date and time values using the datetime library. This library provides datetime objects that can store such data. The dateutil, arrow libraries also allow us to […]

  • 25 October

    Get First Day of Month in Python

    Table of ContentsGet First Day of Month in PythonUsing the datetime.replace() FunctionUsing the datetime.strftime() FunctionUsing the datetime.timedelta ObjectUsing the arrow LibraryConclusion The datetime objects defined in the datetime library provide a convenient way to store date and time values. Other libraries like dateutil also provide extended functionalities that can be used with such objects. In […]

Leave a Reply

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

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.