如何在Python中获取周数?

问题描述 投票:267回答:13

如何使用Python找出6月16日(wk24)当前年份的周数?

python datetime week-number
13个回答
332
投票

datetime.date有一个isocalendar()方法,它返回一个包含日历周的元组:

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

qazxsw poi是一个实例方法,它返回一个元组,该元组包含给定日期实例的相应顺序的年,周数和工作日。


5
投票

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


2
投票

isocalendar()为某些日期返回不正确的年份和周数值:

datetime.datetime.isocalendar

与Mark Ransom的方法比较:

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

2
投票

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

  1. 将原始格式转换为>>> 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 对象。
  2. 使用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) 对象,我们可以使用datetimedatetime.datetime(2017,5,3)

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

或者,更方便的方法是使用official documentation模块。例如dateparsedateparser.parse('16 Jun 2010')dateparser.parse('12/2/12')

以上两种方法将返回一个dateparser.parse('2017-5-3')对象。

第二步

使用获得的datetime对象来调用datetime。例如,

```蟒蛇

strptime(format)

```

决定使用哪种格式非常棘手。更好的方法是让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. 对象调用date。例如,

```蟒蛇

isocalendar()

```

实际上,您将更有可能使用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 准备每周报告,特别是在“圣诞节 - 新年”购物季节。


0
投票
date.isocalendar()

79
投票

您可以直接从datetime获取周数作为字符串。

datetime.date.isocalendar()

此外,您可以获得更改>>> import datetime >>> datetime.date(2010, 6, 16).strftime("%V") '24' 参数的年份周数的不同“类型”:

strftime - 作为零填充十进制数的年份周数(星期日作为一周的第一天)。在第一个星期日之前的新年中的所有日期都被认为是在第0周。例如:00,01,...,53

%U - 一年中的周数(星期一作为一周的第一天)作为十进制数。在第一个星期一之前的新年中的所有日期都被认为是在第0周。例如:00,01,...,53

[...]

(在Python 3.6中添加,向后移植到某些发行版的Python 2.7)为方便起见,包含了C89标准不需要的其他几个指令。这些参数均对应于ISO 8601日期值。当与%W方法一起使用时,这些可能并非在所有平台上都可用。

[...]

strftime() - ISO 8601周作为十进制数,星期一作为一周的第一天。第01周是包含1月4日的一周。例如:01,02,...,53

来自:%V

我从datetime — Basic date and time types — Python 3.7.3 documentation发现了它。它在Python 2.7.6中适用于我


69
投票

我相信here将成为答案。 date.isocalendar()解释了ISO 8601日历背后的数学。查看Python文档的This article的date.isocalendar()部分。

datetime page

.isocalendar()返回一个3元组(年,周数,周日)。 >>> dt = datetime.date(2010, 6, 16) >>> wk = dt.isocalendar()[1] 24 返回年份,dt.isocalendar()[0]返回周数,dt.isocalendar()[1]返回工作日。很简单就可以了。


26
投票

这是另一种选择:

dt.isocalendar()[2]

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

见:24


20
投票

其他人建议的ISO周是好的,但它可能不符合您的需求。它假定每周从星期一开始,这导致在年初和年末出现一些有趣的异常。

如果你宁愿使用一个定义,即第1周始终是1月1日到1月7日,不管星期几,都使用这样的推导:

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

16
投票

通常获取当前周数(从星期日开始):

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

12
投票

对于一年中瞬时周的整数值,请尝试:

from datetime import *
today = datetime.today()
print today.strftime("%U")

8
投票

如果您只是全面使用isocalendar周数,则以下内容应足够:

import datetime
datetime.datetime.utcnow().isocalendar()[1]

这将检索isocalendar为我们的周数返回的元组的第二个成员。

但是,如果您要使用处理公历的日期函数,则仅使用isocalendar不起作用!请看以下示例:

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

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

格里高利历中的第1周是包含星期一的第一周。 isocalendar中的第1周是包含星期四的第一周。 2014年初的部分周包含星期四,因此isocalendar为第1周,第2周为import datetime date = datetime.datetime.strptime("2014-1-1", "%Y-%W-%w") week = date.isocalendar()[1]

如果我们想要获得格里高利周,我们将需要从isocalendar转换为Gregorian。这是一个简单的功能,可以解决问题。

date

6
投票

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

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

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

© www.soinside.com 2019 - 2024. All rights reserved.