As we know date and time are used in programs where we have to keep track of date and time, so it is necessary to have a module to manipulate date and time. In Python, a DateTime module deals with dates and times. Datetime module is built into Python standard library.
Datetime module consists of the following classes:
Name |
Description |
|
---|---|---|
1. | date | It shows the date according to the Georgian calendar with attributes are year, month and day. |
2. | time | It shows time, independent of any particular day with attributes are hour, minute, second, microsecond, and tzinfo. |
3. | DateTime | It is a collection of Dates and Times with the attributes year, month, day, hour, minute, second, microsecond, and tzinfo. |
4. | timedelta | It is used to manipulate Date. |
5. | tzinfo | It provides information about the Time zone. |
Add and subtract days using DateTime in Python
For adding or subtracting Date, we use something called timedelta() function which can be found under the DateTime class. It is used to manipulate Date, and we can perform arithmetic operations on dates like adding or subtracting. timedelta is very easy and useful to implement.
Syntax of DateTime
Syntax: class datetime.timedelta(days=10, seconds=40, microseconds=10, milliseconds=60, minutes=10, hours=4, weeks=8)
Returns : Date
Note : if we doesn’t specify by default it takes integer as an day.
Example 1. Add days from a Current date
We have created a variable called current_date which holds the current date, and then prints that current date. After that, I used timedelta function and in the parameter, We have passed a value that how many days want to add (This value can be any integer).
Python3
from datetime import datetime, timedelta print ( "Current time: " , datetime.now()) print (datetime.now() + timedelta(days = 5 , hours = - 5 )) |
Output:
Current time: 2022-07-13 10:02:04.394152 2022-07-18 05:02:04.394152
Example 2. Subtract days from a Current date
As in the above code, I have created a variable called current_date which holds the current date, and then prints that current date. After that, I used timedelta function and in the parameter, We have passed a value that how many days want to subtract (This value can be any integer).
Python3
from datetime import datetime, timedelta print ( "Current time: " , datetime.now()) print (datetime.now() - timedelta(days = 5 , hours = - 5 )) |
Output:
Current time: 2022-07-13 10:10:27.980591 2022-07-08 15:10:27.981589
Example 3: Subtract months from a Current date
In this example, we have created a self-defined date-time and subtracted 4 months from the current time to get the date-time of the last 4 months ago.
Python3
import datetime from dateutil.relativedelta import relativedelta print ( "Current Date time: " , datetime.datetime( 2022 , 8 , 13 , 10 , 53 , 10 ) ) new_months = my_datetime - relativedelta(months = 4 ) print ( "X Month ago: " , new_months) |
Output:
Current Date time: 2022-08-13 10:53:10 X Month ago: 2022-02-20 07:35:18
Example 4: Add years from a Current date
In this example, we have created a self-defined date-time and added 5 years from the current time to get the date-time after 5 years.
Python3
import datetime from dateutil.relativedelta import relativedelta print ( "Current Date time: " , datetime.datetime( 2022 , 8 , 13 , 10 , 53 , 10 ) ) Year = my_datetime + relativedelta(years = 5 ) print ( "After X Year: " , Year) |
Output:
Current Date time: 2022-08-13 10:53:10 After X Year: 2027-06-20 07:35:18