datetime.timedelta() Function of Python
The 'timedelta()' function of Python is present in the datetime library, which is usually used to calculate differences in given dates. We can also use it to manipulate dates in Python, and this function makes it very easy for users to manipulate dates.
For better understanding, we will show how to use the "datetime.timedelta()" function the following example:
In this example, we will print the future dates of three years and three days:
Initial Date: 2021-10-14 00:09:43.611036 Future Date after Two years: 2023-10-15 00:09:43.611036 Future Date after Two days: 2021-10-17 00:09:43.611036
In this example, we will see how we can print the past dates of two years and two hours:
Initial Date: 2021-10-14 00:19:18.449629 Past date before two years: 2019-10-15 00:19:18.449629 Past date before two hours: 2021-10-13 22:19:18.449629
In this example, we will calculate the difference between the two different dates:
Initial Date: 2021-10-14 00:31:13.920503 New calculated time: 2021-10-21 06:31:25.975503 Time difference between the given dates is: 7 days, 6:00:12.055000 Time difference between the initial Date and New_time_1 is: 14 days, 16:01:30.215000
In this tutorial, we learned about the 'datetime.timedelta()' function of the datetime library in Python. We have also discussed various examples for better understanding the concept of manipulating dates and calculating the difference between two dates.
JavaTpoint offers too many high quality services. Mail us on [email protected], to get more information about given services.
JavaTpoint offers college campus training on Core Java, Advance Java, .Net, Android, Hadoop, PHP, Web Technology and Python. Please mail your requirement at [email protected]
Duration: 1 week to 2 week