擅长:python、mysql、java
<p>根据同样的ISO规范,1月4日总是<em>总是</em>将是某一年的第1周。通过同样的计算,12月28日总是在一年的最后一周。您可以使用它来查找给定年份的最后一周数:</p>
<pre><code>from datetime import date, timedelta
def weeks_for_year(year):
last_week = date(year, 12, 28)
return last_week.isocalendar()[1]
</code></pre>
<p>另请参阅维基百科,ISO周刊文章<a href="http://en.wikipedia.org/wiki/ISO_week_date#Last_week" rel="nofollow noreferrer">lists all properties of the last week</a>:</p>
<blockquote>
<ul>
<li>It has the year's last Thursday in it.</li>
<li>It is the last week with a majority (4 or more) of its days in December.</li>
<li>Its middle day, Thursday, falls in the ending year.</li>
<li>Its last day is the Sunday nearest to 31 December.</li>
<li>It has 28 December in it. Hence the latest possible dates are 28 December through 3 January, the earliest 21 through 28 December.</li>
</ul>
</blockquote>
<p>对于更全面的周计算,可以使用<a href="https://pypi.python.org/pypi/isoweek" rel="nofollow noreferrer">^{<cd1>} module</a>;它有一个<code>Week.last_week_of_year()</code>类方法:</p>
^{pr2}$