Add one year in current date PYTHON -
i have fetched date database following variable
{{ i.operation_date }}
with got value
april 1, 2013
i need add 1 year above, can
april 1, 2014
please suggest, how can this?
agsm's answer shows convenient way of solving problem using python-dateutil
package. if don't want install package? solve problem in vanilla python this:
from datetime import date def add_years(d, years): """return date that's `years` years after date (or datetime) object `d`. return same calendar date (month , day) in destination year, if exists, otherwise use following day (thus changing february 29 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 want other possibility (changing february 29 february 28) last line should changed to:
return d + (date(d.year + years, 3, 1) - date(d.year, 3, 1))
Comments
Post a Comment