× {{alert.msg}} Never ask again
Get notified about new tutorials RECEIVE NEW TUTORIALS

How to Print next year from current year in Python

Martijn Pieters
Sep 05, 2014
<p>Both date and datetime objects have a <code>year</code> attribute, which is a number. Just add 1:</p><pre><code><span style="font-size:14px">&gt;&gt;&gt;</span><span style="font-size:14px"> </span><span style="color:rgb(0, 0, 139); font-size:14px">from</span><span style="font-size:14px"> datetime </span><span style="color:rgb(0, 0, 139); font-size:14px">import</span><span style="font-size:14px"> date </span><span style="font-size:14px">&gt;&gt;&gt;</span><span style="font-size:14px"> </span><span style="color:rgb(0, 0, 139); font-size:14px">print</span><span style="font-size:14px"> date</span><span style="font-size:14px">.</span><span style="font-size:14px">today</span><span style="font-size:14px">().</span><span style="font-size:14px">year </span><span style="font-size:14px">+</span><span style="font-size:14px"> </span><span style="color:rgb(128, 0, 0); font-size:14px">1</span><span style="font-size:14px"> </span><span style="color:rgb(128, 0, 0); font-size:14px">2013</span></code></pre><p>If you have the current year in a variable, just add 1 directly, no need to bother with the datetime module:</p><pre><code><span style="font-size:14px">&gt;&gt;&gt;</span><span style="font-size:14px"> year </span><span style="font-size:14px">=</span><span style="font-size:14px"> </span><span style="color:rgb(128, 0, 0); font-size:14px">2012</span><span style="font-size:14px"> </span><span style="font-size:14px">&gt;&gt;&gt;</span><span style="font-size:14px"> </span><span style="color:rgb(0, 0, 139); font-size:14px">print</span><span style="font-size:14px"> year </span><span style="font-size:14px">+</span><span style="font-size:14px"> </span><span style="color:rgb(128, 0, 0); font-size:14px">1</span><span style="font-size:14px"> </span><span style="color:rgb(128, 0, 0); font-size:14px">2013</span></code></pre><p>If you have the date in a string, just select the 4 digits that represent the year and pass it to <code>int</code>:</p><pre><code><span style="font-size:14px">&gt;&gt;&gt;</span><span style="font-size:14px"> date </span><span style="font-size:14px">=</span><span style="font-size:14px"> </span><span style="color:rgb(128, 0, 0); font-size:14px">'2012-06-26'</span><span style="font-size:14px"> </span><span style="font-size:14px">&gt;&gt;&gt;</span><span style="font-size:14px"> </span><span style="color:rgb(0, 0, 139); font-size:14px">print</span><span style="font-size:14px"> int</span><span style="font-size:14px">(</span><span style="font-size:14px">date</span><span style="font-size:14px">[:</span><span style="color:rgb(128, 0, 0); font-size:14px">4</span><span style="font-size:14px">])</span><span style="font-size:14px"> </span><span style="font-size:14px">+</span><span style="font-size:14px"> </span><span style="color:rgb(128, 0, 0); font-size:14px">1</span><span style="font-size:14px"> </span><span style="color:rgb(128, 0, 0); font-size:14px">2013</span></code></pre><p>Year arithmetic is exceedingly simple, make it an integer and just add 1. It doesn't get much simpler than that.</p><p>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 <code>date</code> object with the year incremented by one:</p><pre><code><span style="font-size:14px">&gt;&gt;&gt;</span><span style="font-size:14px"> today </span><span style="font-size:14px">=</span><span style="font-size:14px"> date</span><span style="font-size:14px">.</span><span style="font-size:14px">today</span><span style="font-size:14px">()</span><span style="font-size:14px"> </span><span style="font-size:14px">&gt;&gt;&gt;</span><span style="font-size:14px"> </span><span style="color:rgb(0, 0, 139); font-size:14px">print</span><span style="font-size:14px"> date</span><span style="font-size:14px">(</span><span style="font-size:14px">today</span><span style="font-size:14px">.</span><span style="font-size:14px">year </span><span style="font-size:14px">+</span><span style="font-size:14px"> </span><span style="color:rgb(128, 0, 0); font-size:14px">1</span><span style="font-size:14px">,</span><span style="font-size:14px"> today</span><span style="font-size:14px">.</span><span style="font-size:14px">month</span><span style="font-size:14px">,</span><span style="font-size:14px"> today</span><span style="font-size:14px">.</span><span style="font-size:14px">day</span><span style="font-size:14px">)</span><span style="font-size:14px"> </span><span style="color:rgb(128, 0, 0); font-size:14px">2013</span><span style="font-size:14px">-</span><span style="color:rgb(128, 0, 0); font-size:14px">06</span><span style="font-size:14px">-</span><span style="color:rgb(128, 0, 0); font-size:14px">26</span></code></pre><p>or you can use the <code>.replace</code> function, which returns a copy with the field you specify changed:</p><pre><code><span style="font-size:14px">&gt;&gt;&gt;</span><span style="font-size:14px"> </span><span style="color:rgb(0, 0, 139); font-size:14px">print</span><span style="font-size:14px"> today</span><span style="font-size:14px">.</span><span style="font-size:14px">replace</span><span style="font-size:14px">(</span><span style="font-size:14px">year</span><span style="font-size:14px">=</span><span style="font-size:14px">today</span><span style="font-size:14px">.</span><span style="font-size:14px">year </span><span style="font-size:14px">+</span><span style="font-size:14px"> </span><span style="color:rgb(128, 0, 0); font-size:14px">1</span><span style="font-size:14px">)</span><span style="font-size:14px"> </span><span style="color:rgb(128, 0, 0); font-size:14px">2013</span><span style="font-size:14px">-</span><span style="color:rgb(128, 0, 0); font-size:14px">06</span><span style="font-size:14px">-</span><span style="color:rgb(128, 0, 0); font-size:14px">26</span></code></pre><p>Note that this can get a little tricky when <code>today</code> is February 29th in a leap year. The absolute, fail-safe correct way to work this one is thus:</p><pre><code><span style="color:rgb(0, 0, 139); font-size:14px">def</span><span style="font-size:14px"> nextyear</span><span style="font-size:14px">(</span><span style="font-size:14px">dt</span><span style="font-size:14px">):</span><span style="font-size:14px"> </span><span style="color:rgb(0, 0, 139); font-size:14px">try</span><span style="font-size:14px">:</span><span style="font-size:14px"> </span><span style="color:rgb(0, 0, 139); font-size:14px">return</span><span style="font-size:14px"> dt</span><span style="font-size:14px">.</span><span style="font-size:14px">replace</span><span style="font-size:14px">(</span><span style="font-size:14px">year</span><span style="font-size:14px">=</span><span style="font-size:14px">dt</span><span style="font-size:14px">.</span><span style="font-size:14px">year</span><span style="font-size:14px">+</span><span style="color:rgb(128, 0, 0); font-size:14px">1</span><span style="font-size:14px">)</span><span style="font-size:14px"> </span><span style="color:rgb(0, 0, 139); font-size:14px">except</span><span style="font-size:14px"> </span><span style="color:rgb(43, 145, 175); font-size:14px">ValueError</span><span style="font-size:14px">:</span><span style="font-size:14px"> </span><span style="color:rgb(128, 128, 128); font-size:14px"># February 29th in a leap year</span><span style="font-size:14px"> </span><span style="color:rgb(128, 128, 128); font-size:14px"># Add 365 days instead to arrive at March 1st</span><span style="font-size:14px"> </span><span style="color:rgb(0, 0, 139); font-size:14px">return</span><span style="font-size:14px"> dt </span><span style="font-size:14px">+</span><span style="font-size:14px"> timedelta</span><span style="font-size:14px">(</span><span style="font-size:14px">days</span><span style="font-size:14px">=</span><span style="color:rgb(128, 0, 0); font-size:14px">365</span><span style="font-size:14px">)</span></code></pre><p>This tip was originally posted on <a href="http://stackoverflow.com/questions/11206489/how-to-print-next-year-from-current-year-in-python/11206511#11206511">Stack Overflow</a>.</p>

Get New Tutorials Delivered to Your Inbox

New tutorials will be sent to your Inbox once a week.

comments powered by Disqus