Python datetime isocalendar 给出了错误的元组
Python datetime isocalendar giving wrong tuple
这是我的 python 代码
import datetime
a = datetime.datetime(2012, 1, 1)
print(a.isocalendar())
输出为
(2011, 52, 7)
输出的格式是一个元组,其中包含年、周数和工作日,分别按给定日期实例的顺序排列。
为什么会这样?不应该是(2012, 1, 1)
吗,如果不是我怎么能把它转换成绝对周数?
根据 documentation of isocalendar
:
Return a 3-tuple, (ISO year, ISO week number, ISO weekday).
The ISO calendar is a widely used variant of the Gregorian calendar. See https://www.staff.science.uu.nl/~gent0113/calendar/isocalendar.htm for a good explanation.
The ISO year consists of 52 or 53 full weeks, and where a week starts on a Monday and ends on a Sunday. The first week of an ISO year is the first (Gregorian) calendar week of a year containing a Thursday. This is called week number 1, and the ISO year of that Thursday is the same as its Gregorian year.
For example, 2004 begins on a Thursday, so the first week of ISO year 2004 begins on Monday, 29 Dec 2003 and ends on Sunday, 4 Jan 2004, so that date(2003, 12, 29).isocalendar() == (2004, 1, 1) and date(2004, 1, 4).isocalendar() == (2004, 1, 7).
在您的情况下,2012 年 1 月 1 日是星期日,因此根据 ISO 日历规则,2012 年的第一周是 1 月 2 日 - 1 月 8 日(因为第一个星期四是 1 月 5 日),而 1 月 1 日属于2011 年的最后一周。
您似乎需要北美周数,而不是 ISO 周数。
>>> a = datetime.datetime(2012, 1, 1)
>>> a.strftime("%Y %U")
'2012 01'
这是我的 python 代码
import datetime
a = datetime.datetime(2012, 1, 1)
print(a.isocalendar())
输出为
(2011, 52, 7)
输出的格式是一个元组,其中包含年、周数和工作日,分别按给定日期实例的顺序排列。
为什么会这样?不应该是(2012, 1, 1)
吗,如果不是我怎么能把它转换成绝对周数?
根据 documentation of isocalendar
:
Return a 3-tuple, (ISO year, ISO week number, ISO weekday).
The ISO calendar is a widely used variant of the Gregorian calendar. See https://www.staff.science.uu.nl/~gent0113/calendar/isocalendar.htm for a good explanation.
The ISO year consists of 52 or 53 full weeks, and where a week starts on a Monday and ends on a Sunday. The first week of an ISO year is the first (Gregorian) calendar week of a year containing a Thursday. This is called week number 1, and the ISO year of that Thursday is the same as its Gregorian year.
For example, 2004 begins on a Thursday, so the first week of ISO year 2004 begins on Monday, 29 Dec 2003 and ends on Sunday, 4 Jan 2004, so that date(2003, 12, 29).isocalendar() == (2004, 1, 1) and date(2004, 1, 4).isocalendar() == (2004, 1, 7).
在您的情况下,2012 年 1 月 1 日是星期日,因此根据 ISO 日历规则,2012 年的第一周是 1 月 2 日 - 1 月 8 日(因为第一个星期四是 1 月 5 日),而 1 月 1 日属于2011 年的最后一周。
您似乎需要北美周数,而不是 ISO 周数。
>>> a = datetime.datetime(2012, 1, 1)
>>> a.strftime("%Y %U")
'2012 01'