The Wayback Machine - https://web.archive.org/web/20240113082140/https://www.geeksforgeeks.org/how-to-add-time-onto-a-datetime-object-in-python/
Open In App
Related Articles

How to add time onto a DateTime object in Python

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

In this article, we will discuss the addition of time onto a specified DateTime object in Python. The effect of this will produce a new DateTime object. This addition can be performed by using datetime.timedelta() function. The timedelta() function is used for calculating differences in dates and also can be used for date manipulations in Python. It is one of the easiest ways to perform date manipulations.

Syntax: datetime.timedelta(days=0, seconds=0, microseconds=0, milliseconds=0, minutes=0, hours=0, weeks=0)

Return values: This function returns the manipulated date.

Thus by simply passing an appropriate value to the above-given parameters, the required task can be achieved.

Example 1: Adding time to DateTime object

Python3




# Python3 code to illustrate the addition
# of time onto the datetime object
  
# Importing datetime
import datetime
  
# Initializing a date and time
date_and_time = datetime.datetime(2021, 8, 22, 11, 2, 5)
  
print("Original time:")
print(date_and_time)
  
# Calling the timedelta() function 
time_change = datetime.timedelta(minutes=75)
new_time = date_and_time + time_change
  
# Printing the new datetime object
print("changed time:")
print(new_time)


Output:

Original time:
2021-08-22 11:02:05
changed time:
2021-08-22 12:17:05

Example 2: Changing day by adding time to DateTime

Python3




# Python3 code to illustrate the addition
# of time onto the datetime object
  
# Importing datetime
import datetime
  
# Initializing a date and time
date_and_time = datetime.datetime(2021, 8, 22, 11, 2, 5)
  
print("Original time:")
print(date_and_time)
  
# Calling the timedelta() function
time_change = datetime.timedelta(hours=36)
new_time = date_and_time + time_change
  
# Printing the new datetime object
print("changed time:")
print(new_time)


Output:

Original time:
2021-08-22 11:02:05
changed time:
2021-08-23 23:02:05

Example 3: Changing two parameters at the same time

Python3




# Python3 code to illustrate the addition
# of time onto the datetime object
  
# Importing datetime
import datetime
  
# Initializing a date and time
date_and_time = datetime.datetime(2021, 8, 22, 11, 2, 5)
  
print("Original time:")
print(date_and_time)
  
# Calling the timedelta() function and
# adding 2 minutes and 10 seconds
time_change = datetime.timedelta(minutes=2, seconds=10)
new_time = date_and_time + time_change
  
# Printing the new datetime object
print("changed time:")
print(new_time)


Output:

Original time:
2021-08-22 11:02:05
changed time:
2021-08-22 11:04:15

Don't miss your chance to ride the wave of the data revolution! Every industry is scaling new heights by tapping into the power of data. Sharpen your skills, become a part of the hottest trend in the 21st century.
Dive into the future of technology - explore the Complete Machine Learning and Data Science Program by GeeksforGeeks and stay ahead of the curve.

Commit to GfG's Three-90 Challenge! Purchase a course, complete 90% in 90 days, and save 90% cost click here to explore.
Last Updated : 23 Sep, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads
Complete Tutorials