Signup/Sign In
LAST UPDATED: DECEMBER 11, 2019

timedelta method in Python

    The timedelta method in Python is present inside the datetime library, and is used for date manipulation purposes. It is considered to be one of the simplest methods for performing the date manipulations. In this article, we will be covering various use cases where you can use the timedelta method.


    Syntax of timedelta method

    datetime.timedelta(days=0, seconds=0, microseconds=0, milliseconds=0, minutes=0, hours=0, weeks=0)

    It has the option of taking multiple parameters and it returns the date based on the parameter values which are passed to it.

    We will look at a few ways of using the timedelta method.




    1. How to get the past date which occurred prior to the current date?

    The date prior to the current date can be obtained by subtracting the number of days from the current date. This can be done using the timedelta() method with days parameter set equal to number of days and then subtracting the result from the current date.


    Time for an example:

    from datetime import datetime, timedelta
    
    days = 2
    date_N_days_ago = datetime.now() - timedelta(days=days)
    print(datetime.now())
    print(date_N_days_ago)

    Output:

    2019-12-11 11:20:36.161473
    2019-12-09 11:20:36.161473
    



    2. How to get the future date which is ahead of the current date?

    The date following the current date can be obtained by adding the number of days to the current date. This can also be achieved with the help of the timedelta() method.


    Time for an example:

    from datetime import datetime, timedelta
    
    days = 700
    date_N_days_ago = datetime.now() + timedelta(days=days)
    print(datetime.now())
    print(date_N_days_ago)

    Output:

    2019-10-28 11:21:13.601299
    2021-09-27 11:21:13.601299
    



    3. Getting the difference between two dates

    Although this is exactly not a use case for the timedelta() function but yes, we can even find the difference between two datetime objects by simply subtracting them using the minus(-) operator.


    Time for an example:

    from datetime import datetime, timedelta
    
    initial_time = datetime.now()  # get the current date
    print ("Initial date is", str(initial_time))
    
    new_final_time = initial_time + timedelta(days = 20)
    print ("new_final_time", str(new_final_time))
    
    print('Time difference:', str(new_final_time - initial_time))

    Output:

    Initial date is 2019-10-28 11:23:20.701701
    new_final_time 2019-11-17 11:23:20.701701
    Time difference: 20 days, 0:00:00
    



    Conclusion

    In this post, we saw how the timedelta() method can be used to get a future date which is a certain given day/time ahead of the current date and past dates too. You can even find dates in future and past which is a few minutes, hours, seconds ahead or in past to the current date using this method. Don't forget to experiment with this method. Come up with interesting observations and let us know in the comment section below.

    I love writing about Python and have more than 5 years of professional experience in Python development. I like sharing about various standard libraries in Python and other Python Modules.
    IF YOU LIKE IT, THEN SHARE IT
    Advertisement

    RELATED POSTS