How can I print the next year if the current year is given in python using the simplest code, possibly in one line using datetime module.
-
1i will get '2005' from the following code 'time.strftime('%Y', time.strptime('2005-11-11', '%Y-%m-%d'))'..now what i need is to get '2006' without much change to this code – Alchemist777 Jun 26 '12 at 11:39
-
1`time.strptime('2005-11-11', '%Y-%m-%d').tm_year + 1` – icecrime Jun 26 '12 at 11:42
2 Answers
Both date and datetime objects have a year
attribute, which is a number. Just add 1:
>>> from datetime import date
>>> print date.today().year + 1
2013
If you have the current year in a variable, just add 1 directly, no need to bother with the datetime module:
>>> year = 2012
>>> print year + 1
2013
If you have the date in a string, just select the 4 digits that represent the year and pass it to int
:
>>> date = '2012-06-26'
>>> print int(date[:4]) + 1
2013
Year arithmetic is exceedingly simple, make it an integer and just add 1. It doesn't get much simpler than that.
If, however, you are working with a whole date, and you need the same date but one year later, use the components to create a new date
object with the year incremented by one:
>>> today = date.today()
>>> print date(today.year + 1, today.month, today.day)
2013-06-26
or you can use the .replace
function, which returns a copy with the field you specify changed:
>>> print today.replace(year=today.year + 1)
2013-06-26
Note that this can get a little tricky when today
is February 29th in a leap year. The absolute, fail-safe correct way to work this one is thus:
def nextyear(dt):
try:
return dt.replace(year=dt.year+1)
except ValueError:
# February 29th in a leap year
# Add 365 days instead to arrive at March 1st
return dt + timedelta(days=365)

- 1,048,767
- 296
- 4,058
- 3,343
-
2The `.replace` function won't work reliably. `d = date(2012, 2, 29)` `d.replace(year=d.year+1)` throws `ValueError: day is out of range for month` and neither will your previous option for building a new date object handle this case. – Duncan Jun 26 '12 at 13:50
-
1@Duncan: nice one; the question is then, what is February 29th one year later? March 1st? – Martijn Pieters Jun 26 '12 at 13:51
-
2Usually March 1st but there could be exceptions. If you take out a 1 year bond on 29th Feb then I think it will mature March 1st the following year. Of course once you get into money calculations it actually will mature on the first working day that is at least one year off skipping over weekends and bank holidays. – Duncan Jun 26 '12 at 13:54
-
@Duncan: For most purposes March 1st is probably 'correct enough'; updated to use a try-except on ValueError, as leap days are a relatively rare occasion. – Martijn Pieters Jun 26 '12 at 14:18
-
1@Duncan: [`date(2012, 2, 29) + relativedelta(years=+1)` is `2013-02-28`](http://stackoverflow.com/a/32812788/4279) (the last day of the month translates into the last day of the month). – jfs Sep 27 '15 at 21:44
here is another simple way...
import datetime
x = datetime.datetime.now()
print(x.year+1)

- 11
- 2
-
The question was about getting the *next* year given the current year. – Gino Mempin Jul 23 '20 at 06:44