Table of Contents
- datetime in Python
- Ways to add hours to datetime in Python
- Using the datetime.Hour Attribute to Add Hours to datetime in Python
- Using the datetime.timedelta object to add hours to datetime in Python
- Using the dateutil.Relativedelta Object to Add Hours to datetime in Python
- Using the pandas.Date Offset() Function to Add Hours to datetime in Python
- Using the arrow.shift() function to add hours to arrow object in Python
- Conclusion
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,
1 2 3 4 5 6 |
from datetime import datetime d = datetime(2022,6,10,16,20,15) print(d) print(d.hour) |
Output:
16
Further reading:
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.
1 2 3 4 5 6 7 |
from datetime import datetime from datetime import timedelta d1 = datetime(2022,5,8,15,20,15) d2 = d1 + timedelta(hours = 2) print(d2) |
Output:
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.
1 2 3 4 5 6 |
from datetime import datetime from dateutil.relativedelta import relativedelta d = datetime(2022,6,10,16,20,15) + relativedelta(hours = 2) print(d) |
Output:
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,
1 2 3 4 5 6 7 |
from datetime import datetime import pandas as pd d1 = datetime(2022,5,8,15,20,15) d2 = d1 + pd.DateOffset(hours = 2) print(d2) |
Output:
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,
1 2 3 4 5 6 |
import arrow t = arrow.now() new_t = t.shift(hours=2) print(new_t) |
Output:
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.