简体   繁体   English

从星期数获取星期开始日期

[英]Get week start date from week number

This question is related to Get date from week number , and is possibly a duplicate of the latter, however, I think what is suggested in the accepted answer to that question does not really work. 这个问题与从星期数获取日期有关,并且可能是后者的重复,但是,我认为在该问题的公认答案中提出的建议实际上是行不通的。

In [6]: datetime.datetime.strptime('2019-18-1', "%Y-%W-%w")
Out[6]: datetime.datetime(2019, 5, 6, 0, 0)

Notice how it returns Monday 2019-5-6. 注意它如何返回2019-5-6星期一。 However, according to the calendar (I use http://whatweekisit.org for reference), 2019-5-6 the Monday of week 19. 但是,根据日历(我使用http://whatweekisit.org作为参考)是2019年5月6日第19周的星期一。

Similarly, the example provided in the original question: 同样,原始问题中提供的示例:

In [7]: datetime.datetime.strptime('2013-26-1', "%Y-%W-%w")
Out[7]: datetime.datetime(2013, 7, 1, 0, 0)

According to http://whatweekisit.org/calendar-2013.html 2013-7-1 is the Monday of week 27. 根据http://whatweekisit.org/calendar-2013.html的说明, 2013-7-1是第27周的星期一。

Also

In [8]: datetime.datetime.strptime('2019-18-1', "%Y-%W-%w").isocalendar()[1]
Out[8]: 19

Notice how I give week 18 to strptime , and get week 19 back from isocalendar . 请注意,我如何将第18周分配给strptime ,并使第19周从isocalendar

I am completely lost and would very much appreciate if someone could explain what is going on here. 我完全迷失了,如果有人可以解释这里的情况,我将不胜感激。 My original goal though is to get week start date from week number. 我最初的目标是从星期数获取星期开始日期。

Based off of my testing, datetime does not consider the first week of 2019 (ie Jan 1-Jan 6) as week 1 because it isn't a full week; 根据我的测试, datetime不将2019年的第一周(即1月1日至1月6日)视为第1周,因为它不是整周。 December 31st, 2018 is part of the week but is not in 2019. I suppose you'll have to accomodate for that by checking the output of datetime.datetime.strptime('year-1-1', "%Y-%W-%w") == datetime.datetime.strptime('year-0-1', "%Y-%W-%w") . 2018年12月31日是一周的一部分,但不在2019年。我想您必须通过检查datetime.datetime.strptime('year-1-1', "%Y-%W-%w") == datetime.datetime.strptime('year-0-1', "%Y-%W-%w") If false, subtract 1. 如果为假,则减去1。

2018 is an example of a year where datetime does return the same value as isocalendar because the first Monday of the year is Jan 1. 2018年是一个示例,其中datetime返回与isocalendar相同的值,因为该年的第一个星期一是1月1日。

From the isocalendar docs : 从isocalendar docs

The ISO year consists of 52 or 53 full weeks, and where a week starts on a Monday and ends on a Sunday. ISO年度包括52或53个整周,其中一个星期从星期一开始,在星期日结束。 The first week of an ISO year is the first (Gregorian) calendar week of a year containing a Thursday. ISO年的第一周是包含星期四的一年中的第一个(格里高利历)日历周。 This is called week number 1, and the ISO year of that Thursday is the same as its Gregorian year. 这称为第1周,该周四的ISO年与其公历年相同。


On the other hand, strptime starts from the first full week, in fact, since 2019 starts from Tuesday, they start from different weeks: 另一方面, strptime从第一个完整的星期开始,实际上,由于2019从星期二开始,所以它们从不同的星期开始:

import datetime as dt
strp_first = dt.datetime.strptime('2019-1-1', "%Y-%W-%w")

>>> print(strp_first)
2019-01-07 00:00:00

>>> print(strp_first.isocalendar()[1])
2

While in 2021, which starts from Friday: 在2021年(从星期五开始):

strp_first = dt.datetime.strptime('2021-1-1', "%Y-%W-%w")

>>> print(strp_first)
2021-01-04 00:00:00

>>> print(strp_first.isocalendar()[1])
1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM