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


当前回答

假设您需要将一周与当天的年份组合为字符串。

import datetime
year,week = datetime.date.today().isocalendar()[:2]
week_of_the_year = f"{year}-{week}"
print(week_of_the_year)

你可能会在2021-28年

其他回答

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

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)

我将讨论总结为两个步骤:

将原始格式转换为datetime对象。 使用datetime对象或date对象的函数来计算周数。

热身

from datetime import datetime, date, time
d = date(2005, 7, 14)
t = time(12, 30)
dt = datetime.combine(d, t)
print(dt)

第一步

要手动生成一个datetime对象,可以使用datetime.datetime(2017,5,3)或datetime.datetime.now()。

但实际上,我们通常需要解析一个现有的字符串。我们可以使用strptime函数,例如datetime.strptime('2017-5-3','%Y-%m-%d'),其中必须指定格式。不同格式代码的详细信息可以在官方文档中找到。

或者,更方便的方法是使用dateparse模块。例如dateparser。parse('16 Jun 2010'), dateparser.parse('12/2/12')或dateparser.parse('2017-5-3')

以上两种方法将返回一个datetime对象。

第二步

使用获得的datetime对象调用strptime(format)。例如,

python

dt = datetime.strptime('2017-01-1','%Y-%m-%d') # return a datetime object. This day is Sunday
print(dt.strftime("%W")) # '00' Monday as the 1st day of the week. All days in a new year preceding the 1st Monday are considered to be in week 0.
print(dt.strftime("%U")) # '01' Sunday as the 1st day of the week. All days in a new year preceding the 1st Sunday are considered to be in week 0.
print(dt.strftime("%V")) # '52' Monday as the 1st day of the week. Week 01 is the week containing Jan 4.

决定使用哪种格式是很棘手的。更好的方法是获取一个日期对象来调用iscalendar()。例如,

python

dt = datetime.strptime('2017-01-1','%Y-%m-%d') # return a datetime object
d = dt.date() # convert to a date object. equivalent to d = date(2017,1,1), but date.strptime() don't have the parse function
year, week, weekday = d.isocalendar() 
print(year, week, weekday) # (2016,52,7) in the ISO standard

实际上,您更有可能使用date. iscalendar()来准备周报,特别是在圣诞节-新年购物季。

这是另一个选择:

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

你可以尝试%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