How to Add Time Onto a Datetime Object in Python

Rate this post

Problem: Given a datetime.datetime object in Python, is there a way to add time to the given datetime object?

Related Question on StackOverflow:

Discussion: Adding time to a datetime object in Python should lead to a new datetime object with the updated time information. For example, if you add 5 hours to a datetime object containing 2022-11-05 12:00:00 should create a new datetime object with 2022-11-05 17:00:00.

Hence, we will be discussing the different approaches that will help us to achieve the above scenario.

Video Walkthrough

How to Add Time Onto a Datetime Object in Python?

Method 1: Using timedelta

The datetime module in Python allows you to manipulate datetime objects with the help of numerous classes like date, time, timedelta, tzinfo, etc. Thus, we will use the timedelta() function from the datetime module to manipulate/add time to the given datetime object.

Approach: You can add time to the datetime object in a two-step process:

  • Create a datetime.timedelta object by calling datetime.timedelta(duration=n).
    • The duration can be milliseconds, microseconds, seconds, minutes, hours, days or weeks. In our example duration is hours.
    • The value of n represents the time specified, which in our example is 5.
  • Add the timedelta object to the datetime object to create a new datetime object with the added time.

Code:

import datetime
original_time = datetime.datetime(2020, 2, 19, 12, 0, 0)
print("Given Datetime: ", original_time)
time_change = datetime.timedelta(hours=5)
new_time = original_time + time_change
print("Changed Datetime: ", new_time)

Output:

Given Datetime:  2020-02-19 12:00:00
Changed Datetime:  2020-02-19 17:00:00

Example 2: Here’s another example showcasing how you can add 1 day, 10 hours and 40 minutes to the given datetime object.

import datetime
import pandas as pd
original_time = datetime.datetime(2020, 2, 19, 12, 0, 0)
print("Given Datetime: ", original_time)
time_change = datetime.timedelta(days=1, hours=10, minutes=40)
new_time = original_time + time_change
print("Changed Datetime: ", new_time)

Output:

Given Datetime:  2020-02-19 12:00:00
Changed Datetime:  2020-02-20 22:40:00

Method 2: Using Pandas

The DateOffset function from the Pandas library allows you to add a specific length of duration to datetime objects. It is generally used to increment or decrement a given timestamp.

Approach:

  • Call the pandas.DateOffset(duration=n) function where the duration can be milliseconds, microseconds, seconds, minutes, hours, days or weeks, and n denotes the length of the duration.
  • Add the newly created DateOffset object to the datetime object to get the new time.

Code:

import datetime
import pandas as pd
original_time = datetime.datetime(2020, 2, 19, 12, 0, 0)
print("Given Datetime: ", original_time)
time_change = pd.DateOffset(hours=5)
new_time = original_time + time_change
print("Changed Datetime: ", new_time)

Output:

Given Datetime:  2020-02-19 12:00:00
Changed Datetime:  2020-02-19 17:00:00

Learn Pandas the Fun Way by Solving Code Puzzles
If you want to boost your Pandas skills, consider checking out my puzzle-based learning book Coffee Break Pandas (Amazon Link).

Coffee Break Pandas Book

Method 3: Using relativedelta

The relativedelta object type from the dateutil module has been designed so that it can be applied to an existing datetime object and can replace specific components of that datetime object.

The following code explains how you can use the relativedelta object to add a specific length of duration to a given datetime object.

Code:

import datetime
from dateutil.relativedelta import relativedelta
original_time = datetime.datetime(2020, 2, 19, 12, 0, 0)
print("Given Datetime: ", original_time)
time_change = relativedelta(hours=5)
new_time = original_time + time_change
print("Changed Datetime: ", new_time)

Output:

Given Datetime:  2020-02-19 12:00:00
Changed Datetime:  2020-02-19 17:00:00

Method 4: Using arrow

What if you want to compute the current date and time and then add a specific interval of time to the current datetime object? Well! You can definitely use the datetime module for this purpose, but Python also provides us with a magnificent library known as arrow when it comes to modifying current datetime objects.

Readers Digest

Here’s an example that demonstrates how to add 5 hours to the current datetime:

import arrow
time_now = arrow.now()
new_time = time_now.shift(hours=5)
print("Current DateTime: ", time_now)
print("Changed DateTime: ", new_time)

Output:

Current DateTime:  2022-05-11T12:20:46.252611+05:30
Changed DateTime:  2022-05-11T17:20:46.252611+05:30

The shift method of the arrow module allows us to shift the time backward or forward by a specific interval.

Bonus: Related Questions

Now that we have gone through numerous methods to add time onto a datetime object in Python let us have a look at some of the frequently asked questions that are related to our above discussion.

How to Create a DateTime Equal to 15 Minutes Ago? | Python Time Substraction

Solution:

import datetime
print(datetime.datetime.now() - datetime.timedelta(minutes=15))

How to Combine datetime.date and datetime.time Objects in Python?

Let’s consider that you have two objects that represent the same event instance, such that one object holds the date of the event while the other object holds the time, and you want to create a datetime object using the two.

Solution:

The easiest solution to this problem is to use the combine() method upon the date and the time objects to club them together into a single datetime object.

import datetime

dt = datetime.datetime(2022, 10, 5)
tm = datetime.time(12, 00)
date_time = dt.combine(dt, tm)
print(date_time)

Output:

2022-10-05 12:00:00

Conclusion

To sum things up, there are majorly three ways which help you to add time onto a date-time object –

  • Use the timedelta() function of the datetime module, or
  • Use the relativedelta class of the dateutil module, or
  • Use the DataOffset function from the Pandas library.

Related Tutorial: How to Get the Current Time in Python?


  • One of the most sought-after skills on Fiverr and Upwork is web scraping. Make no mistake: extracting data programmatically from websites is a critical life skill in today’s world that’s shaped by the web and remote work.
  • So, do you want to master the art of web scraping using Python’s BeautifulSoup?
  • If the answer is yes – this course will take you from beginner to expert in Web Scraping.