How to add time onto a DateTime object in Python
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)
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning - Basic Level Course
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
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
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
Original time: 2021-08-22 11:02:05 changed time: 2021-08-22 11:04:15