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


当前回答

你可以尝试%W指令,如下所示:

d = datetime.datetime.strptime('2016-06-16','%Y-%m-%d')
print(datetime.datetime.strftime(d,'%W'))

'%W':以十进制数表示的一年中的周数(星期一作为一周的第一天)。新年中第一个星期一之前的所有日子都被认为是第0周。(00, 01,…53)

其他回答

别人建议的ISO周是很好的,但可能不适合你的需求。它假设每周从星期一开始,这导致了年初和年底的一些有趣的异常情况。

如果你宁愿使用一个定义,说第一周总是1月1日到1月7日,而不管星期几,可以使用这样的推导:

>>> testdate=datetime.datetime(2010,6,16)
>>> print(((testdate - datetime.datetime(testdate.year,1,1)).days // 7) + 1)
24

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

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)

我们也有类似的问题,于是我们想出了这个逻辑 我已经测试了1年的测试用例,并且全部通过

import datetime


def week_of_month(dt):

    first_day = dt.replace(day=1)
    dom = dt.day
    if first_day.weekday() == 6:
        adjusted_dom = dom
    else:
        adjusted_dom = dom + first_day.weekday()
    if adjusted_dom % 7 == 0 and first_day.weekday() != 6:
       value = adjusted_dom / 7.0 + 1
    elif first_day.weekday() == 6 and adjusted_dom % 7 == 0 and adjusted_dom == 7:
        value = 1
    else:
        value = int(ceil(adjusted_dom / 7.0))

    return int(value)


year = 2020
month = 01
date = 01

date_value = datetime.datetime(year, month, date).date()
no = week_of_month(date_value)

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

我发现这是获得周数的最快方法;所有的变体。

from datetime import datetime


dt = datetime(2021, 1, 3)  # Date is January 3rd 2021 (Sunday), year starts with Friday

dt.strftime("%W")  # '00'; Monday is considered first day of week, Sunday is the last day of the week which started in the previous year
dt.strftime("%U")  # '01'; Sunday is considered first day of week
dt.strftime("%V")  # '53'; ISO week number; result is '53' since there is no Thursday in this year's part of the week

%V的进一步说明可以在Python文档中找到:

国际标准化组织的一年包括52或53个完整的星期,其中一周从星期一开始,到星期日结束。ISO年的第一周是包含星期四的一年的第一个(公历)周。这被称为第1周,星期四的ISO年份与公历年份相同。

https://docs.python.org/3/library/datetime.html#datetime.date.isocalendar

注意:请记住返回值是一个字符串,因此如果需要一个数字,则将结果传递给int构造函数。