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

  • 18 May

    Get Hour from datetime in Python

    Table of ContentsHow to get hour from datetime in Python?Using the time() function and manually slicing out the hour part to get hour from datetime in Python.Using the % operator along with the datetime.hour attribute to get hour from datetime in Python.Using the format() function along with the datetime.hour attribute to get hour from datetime […]

  • 18 May

    Convert Month Name to Number in Python

    Table of ContentsHow to convert month name to number in Python?Creating a user-defined function and utilizing it to convert month name to number in Python.Using the calendar module to create a dictionary and utilize it to convert month name to number in Python.Using dict comprehension and the calendar module to convert month name to number […]

  • Format Date to YYYYMMDD in Python
    03 March

    Format Date to YYYYMMDD in Python

    Table of ContentsDate formats in PythonHow to convert date to YYYYMMDD format in PythonUsing datetime.strftime() Function to Format Date to YYYYMMDD in PythonUsing datetime.date Object to Format Date to YYYYMMDD in PythonUsing date.isoformat() Function to Format Date to YYYYMMDD in PythonUsing arrow.format() Function to Format Date to YYYYMMDD in PythonUsing pendulum.to_date_string() Function to Format Date […]

  • Get today's date in Python
    03 March

    Get Today’s Date in Python

    Table of ContentsDate and Time objects in PythonGet Today’s Date in PythonUsing date.today() Function to Get Today’s Date in PythonUsing datetime.today() and datetime.strftime() Functions to Get Today’s Date in PythonUsing datetime.now() Function to Get Today’s Date in PythonUsing strftime() Function to Get Today’s Date in PythonUsing time.strftime() Function to Get Today’s Date in PythonUsing arrow.now() […]

  • Add seconds to datetime in Python
    11 February

    Add Seconds to Datetime in Python

    Table of ContentsDate and Time in PythonWays to add seconds to datetime object in PythonUsing the datetime.timedelta object to add seconds to datetime in PythonUsing the datetime.time.second attribute to add seconds to datetime in PythonUsing the arrow.shift() function to add seconds to arrow object in PythonConclusionWas this post helpful? Date and Time in Python Python […]

  • Python get year from date
    23 January

    Get year from Date in Python

    Table of ContentsDates in PythonWays to get year from date stored in a datetime object in PythonUsing the year attributeUsing the strftime() functionUsing the pandas.to_datetime functionWays to get year from date stored as a string in PythonUsing the for loopUsing the re.search()ConclusionWas this post helpful? In this post, we will see how to get year […]

Leave a Reply

Your email address will not be published.

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.