Python | datetime.timedelta() function

Python timedelta() function is present under datetime library which is generally 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)

Returns : Date

Code #1:

filter_none

edit
close

play_arrow

link
brightness_4
code

# Timedelta function demonstration 
  
from datetime import datetime, timedelta
  
  
# Using current time
ini_time_for_now = datetime.now()
  
# printing initial_date
print ("initial_date", str(ini_time_for_now))
  
# Calculating future dates
# for two years
future_date_after_2yrs = ini_time_for_now + \
                        timedelta(days = 730)
  
future_date_after_2days = ini_time_for_now + \
                         timedelta(days = 2)
  
# printing calculated future_dates
print('future_date_after_2yrs:', str(future_date_after_2yrs))
print('future_date_after_2days:', str(future_date_after_2days))

chevron_right


Output:

initial_date 2019-02-27 12:41:45.018389
future_date_after_2yrs: 2021-02-26 12:41:45.018389
future_date_after_2days: 2019-03-01 12:41:45.018389

Code #2:

filter_none

edit
close

play_arrow

link
brightness_4
code

# Timedelta function demonstration 
from datetime import datetime, timedelta
  
  
# Using current time
ini_time_for_now = datetime.now()
  
# printing initial_date
print ("initial_date", str(ini_time_for_now))
  
# Calculating past dates
# for two years
past_date_before_2yrs = ini_time_for_now - \
                       timedelta(days = 730)
  
# for two hours
past_date_before_2hours = ini_time_for_now - \
                        timedelta(hours = 2)
  
  
# printing calculated past_dates
print('past_date_before_2yrs:', str(past_date_before_2yrs))
print('past_date_after_2days:', str(past_date_before_2hours))

chevron_right


Output:

initial_date 2019-02-27 12:41:46.104662
past_date_before_2yrs: 2017-02-27 12:41:46.104662
past_date_after_2days: 2019-02-27 10:41:46.104662

Code #3:

filter_none

edit
close

play_arrow

link
brightness_4
code

# Timedelta function demonstration 
from datetime import datetime, timedelta
  
# Using current time
ini_time_for_now = datetime.now()
  
# printing initial_date
print ("initial_date", str(ini_time_for_now))
  
# Some another datetime
new_final_time = ini_time_for_now + \
                 timedelta(days = 2)
  
# printing new final_date
print ("new_final_time", str(new_final_time))
  
  
# printing calculated past_dates
print('Time difference:', str(new_final_time - \
                             ini_time_for_now))

chevron_right


Output:

initial_date 2019-02-27 12:41:47.386595
new_final_time 2019-03-01 12:41:47.386595
Time difference: 2 days, 0:00:00


My Personal Notes arrow_drop_up

Programming freaktech Enthusiast and have interest in learning new upcoming technologies

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.