Python - Timedelta object with negative values
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.
# importing datetime and timedelta from
# datetime module
from datetime import datetime, timedelta
# function to return date
def get_date(current_date):
# creating timedelta object with negative
# values
date_obj = timedelta(days=-534)
# getting required date
req_date = current_date + date_obj
# splitting date from resultant datetime
date = req_date.date()
# returning date
return date
# main function
if __name__ == '__main__':
# getting current date and time
current_datetime = datetime.now()
# calling function to get the date
resulted_date = get_date(current_datetime)
# printing current date
print('Current date is:', current_datetime.date())
# printing resultant date after using timedelta
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.
# importing datetime and timedelta from
# datetime module
from datetime import datetime, timedelta
# function to return time
def get_time(current_time):
# creating timedelta object with negative
# values
time_obj = timedelta(hours=-12, minutes=-15)
# getting required time
req_time = current_time + time_obj
# splitting time from resultant datetime
time = req_time.time()
# returning time
return time
# main function
if __name__ == '__main__':
# getting current date and time
current_datetime = datetime.now()
# calling function to get the time
resulted_time = get_time(current_datetime)
# printing current time
print('Current time is:', current_datetime.time())
# printing resultant time after using timedelta
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.
# importing datetime and timedelta from
# datetime module
from datetime import datetime, timedelta
# function to return time
def get_time(current_time):
# creating timedelta object with negative
# values
time_obj = timedelta(hours=15, minutes=25)
# getting required time
req_time = current_time - time_obj
# splitting time from resultant datetime
time = req_time.time()
# returning time
return time
# main function
if __name__ == '__main__':
# getting current date and time
current_datetime = datetime.now()
# calling function to get the time
resulted_time = get_time(current_datetime)
# printing current time
print(f'Current time is: {current_datetime.time()}')
# printing resultant time after using timedelta
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.
# importing datetime and timedelta from datetime module
from datetime import datetime,timedelta
# function to return time
def get_datetime(current_datetime):
# creating timedelta object with negative values
time_obj = timedelta(weeks=-1, days=-4,
hours=-15, minutes=-25,
seconds=-54)
# getting required time and time
req_time = current_datetime + time_obj
# returning date and time
return req_time
# main function
if __name__ == '__main__':
# getting current date and time
current_datetime = datetime.now()
# calling function to get the date and time
resulted_time = get_datetime(current_datetime)
# printing current date and time
print(f'Current time is: {current_datetime}')
# printing resultant date and time after using timedelta
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.