如何用Python找出今年6月16日(wk24)的周数?


当前回答

有许多周编号系统。下面是最常见的系统代码示例:

ISO: First week starts with Monday and must contain the January 4th (or first Thursday of the year). The ISO calendar is already implemented in Python: >>> from datetime import date >>> date(2014, 12, 29).isocalendar()[:2] (2015, 1) North American: First week starts with Sunday and must contain the January 1st. The following code is my modified version of Python's ISO calendar implementation for the North American system: from datetime import date def week_from_date(date_object): date_ordinal = date_object.toordinal() year = date_object.year week = ((date_ordinal - _week1_start_ordinal(year)) // 7) + 1 if week >= 52: if date_ordinal >= _week1_start_ordinal(year + 1): year += 1 week = 1 return year, week def _week1_start_ordinal(year): jan1 = date(year, 1, 1) jan1_ordinal = jan1.toordinal() jan1_weekday = jan1.weekday() week1_start_ordinal = jan1_ordinal - ((jan1_weekday + 1) % 7) return week1_start_ordinal >>> from datetime import date >>> week_from_date(date(2014, 12, 29)) (2015, 1) MMWR (CDC): First week starts with Sunday and must contain the January 4th (or first Wednesday of the year). I created the epiweeks package specifically for this numbering system (also has support for the ISO system). Here is an example: >>> from datetime import date >>> from epiweeks import Week >>> Week.fromdate(date(2014, 12, 29)) (2014, 53)

其他回答

iscalendar()对于某些日期返回错误的年和周数值:

Python 2.7.3 (default, Feb 27 2014, 19:58:35) 
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import datetime as dt
>>> myDateTime = dt.datetime.strptime("20141229T000000.000Z",'%Y%m%dT%H%M%S.%fZ')
>>> yr,weekNumber,weekDay = myDateTime.isocalendar()
>>> print "Year is " + str(yr) + ", weekNumber is " + str(weekNumber)
Year is 2015, weekNumber is 1

与Mark Ransom的方法相比:

>>> yr = myDateTime.year
>>> weekNumber = ((myDateTime - dt.datetime(yr,1,1)).days/7) + 1
>>> print "Year is " + str(yr) + ", weekNumber is " + str(weekNumber)
Year is 2014, weekNumber is 52

datetime。Date有一个iscalendar()方法,它返回一个包含日历周的元组:

>>> import datetime
>>> datetime.date(2010, 6, 16).isocalendar()[1]
24

datetime.date. iscalendar()是一个实例方法,返回一个元组,其中包含给定日期实例的年份、周号和星期。

在Python 3.9+中,iscalendar()返回一个包含年份、星期和星期的命名元组,这意味着您可以使用命名属性显式地访问星期:

>>> import datetime
>>> datetime.date(2010, 6, 16).isocalendar().week
24

这是另一个选择:

import time
from time import gmtime, strftime
d = time.strptime("16 Jun 2010", "%d %b %Y")
print(strftime(d, '%U'))

结果是24。

见:http://docs.python.org/library/datetime.html strftime-and-strptime-behavior

如果你只使用等历法周数,那么以下内容就足够了:

import datetime
week = date(year=2014, month=1, day=1).isocalendar()[1]

这将检索由iscalendar为周数返回的元组的第二个成员。

但是,如果您打算使用处理公历的日期函数,那么单独使用iscalendar是行不通的!举个例子:

import datetime
date = datetime.datetime.strptime("2014-1-1", "%Y-%W-%w")
week = date.isocalendar()[1]

这里的字符串表示返回2014年第一周的星期一作为日期。当我们使用iscalendar在这里检索周数时,我们希望得到相同的周数,但我们没有。相反,我们得到的周数是2。为什么?

公历的第一周是包含星期一的第一周。等日历中的第1周是包含星期四的第一周。2014年初的部分周包含一个星期四,所以这是等历的第一周,日期是第2周。

如果我们想要得到公历周,我们将需要从等历法转换为公历。下面是一个简单的函数。

import datetime

def gregorian_week(date):
    # The isocalendar week for this date
    iso_week = date.isocalendar()[1]

    # The baseline Gregorian date for the beginning of our date's year
    base_greg = datetime.datetime.strptime('%d-1-1' % date.year, "%Y-%W-%w")

    # If the isocalendar week for this date is not 1, we need to 
    # decrement the iso_week by 1 to get the Gregorian week number
    return iso_week if base_greg.isocalendar()[1] == 1 else iso_week - 1

对于pandas用户,如果你想获得一列周数:

df['weekofyear'] = df['Date'].dt.week