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


当前回答

已经给出了很多答案,但我想补充一下。

如果您需要将周显示为年/周样式(例如,1953 - 2019年第53周,2001 - 2020年第1周等),您可以这样做:

import datetime

year = datetime.datetime.now()
week_num = datetime.date(year.year, year.month, year.day).strftime("%V")
long_week_num = str(year.year)[0:2] + str(week_num)

它将使用当前的年份和周,写这篇文章当天的long_week_num将是:

>>> 2006

其他回答

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

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

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

我们也有类似的问题,于是我们想出了这个逻辑 我已经测试了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)

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

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构造函数。

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

将原始格式转换为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()来准备周报,特别是在圣诞节-新年购物季。

已经给出了很多答案,但我想补充一下。

如果您需要将周显示为年/周样式(例如,1953 - 2019年第53周,2001 - 2020年第1周等),您可以这样做:

import datetime

year = datetime.datetime.now()
week_num = datetime.date(year.year, year.month, year.day).strftime("%V")
long_week_num = str(year.year)[0:2] + str(week_num)

它将使用当前的年份和周,写这篇文章当天的long_week_num将是:

>>> 2006