Python – Timedelta object with negative values
Last Updated :
20 Aug, 2021
In Python, there is the Datetime library which is the in-built library under which timedelta() function is present. Using this function we can find out the future date and time or past date and time. In timedelta() object delta represents the difference between two dates or times.
Syntax:
datetime.timedelta(days=0, seconds=0, microseconds=0, milliseconds=0, minute=0, hours=0, weeks=0)
All the arguments are optional and are 0 by default. We can pass both positive and negative values in the arguments.
Let’s see some examples for better understanding of the topic.
Example 1: Finding estimate date by using timedelta object with negative values.
Python3
from datetime import datetime, timedelta
def get_date(current_date):
date_obj = timedelta(days = - 534 )
req_date = current_date + date_obj
date = req_date.date()
return date
if __name__ = = '__main__' :
current_datetime = datetime.now()
resulted_date = get_date(current_datetime)
print ( 'Current date is:' , current_datetime.date())
print ( 'Resultant time after using timedelta object is:' ,
resulted_date)
|
Output:
Current date is: 2021-03-24
Resultant time after using timedelta object is: 2019-10-07
The above example shows the current date and the date after using timedelta object, in the above example we had passed days=-534 as a parameter in timedelta object. The negative value represents the past whereas the positive value represents the future, in the above code, we had passed -534 days this means that we are getting a date that is 534 days back from today’s date.
Example 2: Finding estimate time by using timedelta object with negative values.
Python3
from datetime import datetime, timedelta
def get_time(current_time):
time_obj = timedelta(hours = - 12 , minutes = - 15 )
req_time = current_time + time_obj
time = req_time.time()
return time
if __name__ = = '__main__' :
current_datetime = datetime.now()
resulted_time = get_time(current_datetime)
print ( 'Current time is:' , current_datetime.time())
print ( 'Resultant time after using timedelta object is:' ,
resulted_time)
|
Output:
Current time is: 15:53:37.019928
Resultant time after using timedelta object is: 03:38:37.019928
The above example shows the current time and the time after using timedelta object, in the above code we had passed hours=-12 and minutes=-15 that means we are getting resultant time i.e, before 12 hours and 15 minutes from now onwards.
Example 3: Another way of finding estimate time by using timedelta object with negative values.
Python3
from datetime import datetime, timedelta
def get_time(current_time):
time_obj = timedelta(hours = 15 , minutes = 25 )
req_time = current_time - time_obj
time = req_time.time()
return time
if __name__ = = '__main__' :
current_datetime = datetime.now()
resulted_time = get_time(current_datetime)
print (f 'Current time is: {current_datetime.time()}' )
print (f 'Resultant time after using timedelta object is: {resulted_time}' )
|
Output:
Current time is: 15:52:59.538796
Resultant time after using timedelta object is: 00:27:59.538796
In the above example, we had passed the positive values in the timedelta object, but they are behaving like negative values because while finding the required_time at line 9 in the above code we had used the negative sign with timedelta object so the values of the parameters which are passed positive become negative automatically, and we get resultant time i.e, before 15 hours and 25 minutes from now onwards.
Example 4: Finding estimate date and time by using timedelta object with negative values.
Python3
from datetime import datetime,timedelta
def get_datetime(current_datetime):
time_obj = timedelta(weeks = - 1 , days = - 4 ,
hours = - 15 , minutes = - 25 ,
seconds = - 54 )
req_time = current_datetime + time_obj
return req_time
if __name__ = = '__main__' :
current_datetime = datetime.now()
resulted_time = get_datetime(current_datetime)
print (f 'Current time is: {current_datetime}' )
print (f 'Resultant time after using timedelta object is: {resulted_time}' )
|
Output:
Current time is: 2021-03-24 15:51:33.024268
Resultant time after using timedelta object is: 2021-03-13 00:25:39.024268
The above example shows the current date and time and resultant date and time after using timedelta object, in the above code we are passing weeks=-1 days=-4, hours=-15, minutes=-25, seconds=-54 in the timedelta object means we are getting estimated date and time after using timedelta object is 1 week 4 days 15 hours 25 minutes and 54 seconds before from now onwards.
Share your thoughts in the comments
Please Login to comment...