Skill - Add or subtract months or years to datetime object in python
Skills Required
- Setup python development environment
- Basic Printing in Python
- Commenting in Python
- Managing Variables in python
- Python datetime library
Please make sure to have all the skills mentioned above to understand and execute the code mentioned below. Go through the above skills if necessary for reference or revision
datetime
module in python is very useful in manipulating date-times.
calendar
module in python is very useful in performing general calendar related functions
In this post we will learn how to add months to a datetime in python
Let number of months to add to a particular datetime be k
The algorithm we are using is as follows
- Add floor((input month - 1 + k)/12) to input year component to get result year component
- Result month component would be (input month -1 + k)%12 + 1
- Result day component would be minimum of input date component and max date of that result month (For example we cant have day component as 30 in February month)
- hour, minute, seconds and microsecond components of result time will be the same as input datetime
If implementation of above algorithm as a function in python would be
# import datetime and calendar modules
import datetime as dt
import calendar
def addMonths(inpDt, mnths):
tmpMnth = inpDt.month - 1 + mnths
# Add floor((input month - 1 + k)/12) to input year component to get result year component
resYr = inpDt.year + tmpMnth // 12
# Result month component would be (input month - 1 + k)%12 + 1
resMnth = tmpMnth % 12 + 1
# Result day component would be minimum of input date component and max date of the result month (For example we cant have day component as 30 in February month)
# Maximum date in a month can be found using the calendar module monthrange function as shown below
resDay = min(inpDt.day, calendar.monthrange(resYr,resMnth)[1])
# construct result datetime with the components derived above
resDt = dt.datetime(resYr, resMnth, resDay, inpDt.hour, inpDt.minute, inpDt.second, inpDt.microsecond)
return resDt
# let's test our function with current time
nowDt = dt.datetime.now()
print('Now =', end=' ')
print(nowDt)
print('Now + 2 months =', end=' ')
print(addMonths(nowDt, 2))
In the above code, in order to get the maximum date for a given month (mnth) of a year (yr), we use the following function from calendar module
calendar.monthrange(yr, mnth)[1]
You can use this function in your own projects directly from the algorithm implementation code written above
If you want to add years, just multiply years with 12 and use this function
def addYears(inpDt, yrs):
return addMonths(inpDt, yrs*12)
Points to notice
- If we subtract 1 month from 30 Mar or 31 Mar 2020, we will get 29 Feb 2020 as per our algorithm.
- This might not not be the desired behavior in some cases. For example, some people may want the result to be
none
instead of 29 Mar 2020 - You can modify the function in our algorithm implementation code to suit your requirements
Video
Video for this post can be found here
Online Interpreter
You can run these codes online at https://www.programiz.com/python-programming/online-compiler/
References
- Official documentation - https://docs.python.org/3/library/calendar.html
- verified stackoverflow answer - https://stackoverflow.com/questions/4130922/how-to-increment-datetime-by-custom-months-in-python-without-using-library
Comments
Post a Comment