I have fetched a date from database with the following variable
{{ i.operation_date }}
AGSM's answer shows a convenient way of solving this problem using the python-dateutil package. But what if you don't want to install that package? You could solve the problem in vanilla Python like this:
from datetime import date
def add_years(d, years):
"""Return a date that's `years` years after the date (or datetime)
object `d`. Return the same calendar date (month and day) in the
destination year, if it exists, otherwise use the following day
(thus changing February 29 to March 1).
"""
try:
return d.replace(year = d.year + years)
except ValueError:
return d + (date(d.year + years, 1, 1) - date(d.year, 1, 1))
If you want the other possibility (changing February 29 to February 28) then the last line should be changed to:
return d + (date(d.year + years, 3, 1) - date(d.year, 3, 1))
It seems from your question that you would like to simply increment the year of your given date rather than worry about leap year implications. You can use the date class to do this by accessing its member year.
from datetime import date
startDate = date(2012, 12, 21)
# reconstruct date fully
endDate = date(startDate.year + 1, startDate.month, startDate.day)
# replace year only
endDate = startDate.replace(startDate.year + 1)
If you're having problems creating one given your format, let us know.
Here's one more answer that I've found to be pretty concise and doesn't use external packages:
import datetime as dt
# Assuming `day` has type dt.date
one_year_delta = dt.timedelta(days=366 if ((day.month >= 3 and calendar.isleap(day.year+1)) or
(day.month < 3 and calendar.isleap(day.year))) else 365)
day += one_year_delta
convert it into python datetime object if it isn't already. then add deltatime
one_years_later = Your_date + datetime.timedelta(days=(years*days_per_year))
for your case days=365.
you can have condition to check if the year is leap or no and adjust days accordingly
you can add as many years as you want
Look at this:
#!/usr/bin/python
import datetime
def addYears(date, years):
result = date + datetime.timedelta(366 * years)
if years > 0:
while result.year - date.year > years or date.month < result.month or date.day < result.day:
result += datetime.timedelta(-1)
elif years < 0:
while result.year - date.year < years or date.month > result.month or date.day > result.day:
result += datetime.timedelta(1)
print "input: %s output: %s" % (date, result)
return result
Example usage:
addYears(datetime.date(2012,1,1), -1)
addYears(datetime.date(2012,1,1), 0)
addYears(datetime.date(2012,1,1), 1)
addYears(datetime.date(2012,1,1), -10)
addYears(datetime.date(2012,1,1), 0)
addYears(datetime.date(2012,1,1), 10)
And output of this example:
input: 2012-01-01 output: 2011-01-01
input: 2012-01-01 output: 2012-01-01
input: 2012-01-01 output: 2013-01-01
input: 2012-01-01 output: 2002-01-01
input: 2012-01-01 output: 2012-01-01
input: 2012-01-01 output: 2022-01-01
Another way would be to use pandas "DateOffset" class
link:-https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.tseries.offsets.DateOffset.html
Using ASGM's code(above in the answers):
from datetime import datetime
import pandas as pd
your_date_string = "April 1, 2012"
format_string = "%B %d, %Y"
datetime_object = datetime.strptime(your_date_string, format_string).date()
new_date = datetime_object + pd.DateOffset(years=1)
new_date.date()
It will return the datetime object with the added year.
Something like this:-
datetime.date(2013, 4, 1)