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


当前回答

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

将原始格式转换为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

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

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

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

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

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

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

如果您需要将周显示为年/周样式(例如,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