Search Here

Python timedelta

A common scenario involving date and times involve performing mathematical operations on dates and times themselves. For example when you want to calculate number of days, events or you want to find out what is the next date and time than we use timedelta class in python to find out these operations.
Python timedelta


Before going to start you can see that we have already import times and date information using import date, time and python datetime class from python library. For this tutorial we are going to import python timedelta class. After importing these classes now I can use timedetla information. In my previous article we have discussed about python strftime function.

What is timedelta

A timedetla is basically is span of time, its not a particular date, its not a particular time. It's span of time. You can use this class to perform time, date mathematics. So let's take a look some examples.

#!/usr/bin/python3
from datetime import time
from datetime import date
from datetime import datetime
from datetime import timedelta

def main():
    #construct a basic timedelta and print it
    print(timedelta(days=365, hours = 5, minutes=1))
if __name__ == "__main__": main()  
       
 



So to construct a basic timedetla all you do is create a timedetla class and pass in the amount of time that you want to timedelta represent. So this timedetla is going to represent span of time "1 year 5 hours and 1 minute". 

Python timedelta



So that's the span of time that represents time delta.

Next we have printed today's date. You look familiar how to print and manipulate dates and time, if you are new to python datetime manipulation read this article.
Let's use the timedelta to figure out that what today's date will be one year from now.

#!/usr/bin/python3
from datetime import time
from datetime import date
from datetime import datetime
from datetime import timedelta

def main():
    #construct a basic timedelta and print it
    print(timedelta(days=365, hours = 5, minutes=1))
    
    # print today's date
    print("Today is: ",datetime.now())
    #print todays date one year from now
    print("One year from now it will be: ",datetime.now() + timedelta(days=365))

if __name__ == "__main__": main()
       
 



So I am doing here is printing out "One year from now it will be"  and I get the value of "datetime.now()" that is todays date and plus that today's date with the timedelta whose span is equal to one year which mean we have added 365 days to the today's date so that would give me that date in one year. 
Python timedelta

How to use timedelta that takes more than one argument


#!/usr/bin/python3
from datetime import time
from datetime import date
from datetime import datetime
from datetime import timedelta

def main():
    #construct a basic timedelta and print it
    print(timedelta(days=365, hours = 5, minutes=1))
    
    # print today's date
    print("Today is: ",datetime.now())
    #print todays date one year from now
    print("One year from now it will be: ",datetime.now() + timedelta(days=365))
    
    # use timedelta that takes more than one argument
    print("In two weeks and three days it will be: ",datetime.now() + timedelta(weeks = 2, days = 3))
    
if __name__ == "__main__": main()      
       
       
 



In above code we have given two arguments to timedelta as a time span. The first one is week with time span two weeks and the second is days with the time span two days.

Python timedelta



Let's take a date one week ago and formatted the date as a string. 

 
 
#!/usr/bin/python3
from datetime import time
from datetime import date
from datetime import datetime
from datetime import timedelta

def main():
    #construct a basic timedelta and print it
    print(timedelta(days=365, hours = 5, minutes=1))
    
    # print today's date
    print("Today is: ",datetime.now())
    #print todays date one year from now
    print("One year from now it will be: ",datetime.now() + timedelta(days=365))
    
    # use timedelta that takes more than one argument
    print("In two weeks and three days it will be: ",datetime.now() + timedelta(weeks = 2, days = 3))
    
    # calculate the date one week ago, and formatted as a string using strftime
    t = datetime.now() - timedelta(weeks = 1)
    s = t.strftime("%A %B %d, %Y")
    print("One week ago it was: ", s)
    
if __name__ == "__main__": main()      
       
       
 



In above code we have created a variable t and in that variable we have assigning today's date subtract a timedelta that's one week long. Then I used the python strftime function on the resulting datetime object and print it out.

Python timedelta




So using timedelta you can calculate time and dates in future as well as past. You can see by using timedelta you can do some pretty good and advance date calculations. 

Post a Comment

1 Comments