Open In App

How to add time onto a DateTime object in Python

Last Updated : 23 Sep, 2021
Improve
Improve
Like Article
Like
Save
Share
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


Previous Article
Next Article

Similar Reads

Python | Copy and Paste Images onto other Image using Pillow
In this article, we will learn how to copy an image over another image using pillow library. We will use image module from pillow and copy() and paste() methods to achieve this task. We will need to create copies of both images so that it does not affect the original image with the help of copy() method and then paste the image on the other image w
2 min read
Python DateTime - DateTime Class
DateTime class of the DateTime module as the name suggests contains information on both dates as well as time. Like a date object, DateTime assumes the current Gregorian calendar extended in both directions; like a time object, DateTime assumes there are exactly 3600*24 seconds in every day. But unlike the date class, the objects of the DateTime cl
5 min read
How to convert a Python datetime.datetime to excel serial date number
This article will discuss the conversion of a python datetime.datetime to an excel serial date number. The Excel "serial date" format is actually the number of days since 1900-01-00. The strftime() function is used to convert date and time objects to their string representation. It takes one or more inputs of formatted code and returns the string r
3 min read
How to Fix - "datetime.datetime not JSON serializable" in Python?
In this article, we are going to learn how to fix the error "datetime.datetime not JSON serializable" in Python. datetime.datetime is a class in the Python datetime module that represents a single point in time. This class is not natively supported by the JSON (JavaScript Object Notation) format, which means that we cannot serialize a datetime.date
4 min read
Add Years to datetime Object in Python
In this article let's learn how to add years to a datetime object in python. Python Datetime module supplies classes datetime. These classes provide us with a number of functions to deal with dates, times, and time intervals. Date and datetime are an object in Python, so when you manipulate them, you are actually manipulating objects and not string
2 min read
Add Months to datetime Object in Python
In this article, let's delve into the techniques for Add Months to datetime Object in Python. Working with dates and times often requires manipulation and adjustment, and understanding how to add months to a datetime object is a crucial skill. We will explore various methods and libraries to achieve this task, providing you with a comprehensive gui
3 min read
How to convert timestamp string to datetime object in Python?
Python has a module named Datetime to work with dates and times. We did not need to install it separately. It is pre-installed with the Python package itself. A UNIX timestamp is several seconds between a particular date and January 1, 1970, at UTC. Input: "2023-07-21 10:30:45"Output: 2023-07-21 10:30:45Explanation: In This, We are converting a tim
2 min read
How to convert Python's .isoformat() string back into datetime object
In this article, we will learn how to convert Pythons .isoFormat() string back into a datetime object. Here we are using the current time and for that, we will store the current time in the current_time variable. The function now() of this module, does the job perfectly. Example: Getting current time C/C++ Code from datetime import datetime current
1 min read
How to remove timezone information from DateTime object in Python
Timezone is defined as a geographical area or region throughout which standard time is observed. It basically refers to the local time of a region or country. Most of the time zones are offset from Coordinated Universal Time (UTC), the world’s standard for time zone. In this article, we will discuss how to remove timezone information from the DateT
2 min read
How to make a timezone aware datetime object in Python
In this example, we are going to see how to make a timezone-aware DateTime object in Python. Timezone-aware objects are Python DateTime or time objects that include timezone information. An aware object represents a specific moment in time that is not open to interpretation. Checking if an object is timezone aware or not: We can easily check if a d
4 min read
Practice Tags :
three90RightbarBannerImg