Python:一个月中的周数
指定日期时:
datetime.datetime(2011, 8, 15)
我如何知道这是该月的第三周?
如果我想要这个日期怎么办?
datetime.datetime(2011, 2, 28) //should return 4
datetime.datetime(2011, 8, 29) //should return 5
据我了解,如果给定日期是闰年(双六分相),则只有 2 月有 4 周
When specifying a date:
datetime.datetime(2011, 8, 15)
How can i get to know that this is the third week of the month?
What if I want this dates?
datetime.datetime(2011, 2, 28) //should return 4
datetime.datetime(2011, 8, 29) //should return 5
I understand that only February has 4 weeks if the given date is in a leap year (bissextile)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
其他答案中显示的模式方法可能会产生误导。想象一下一年中的几周。一年 365 天,总共有 52 个 7 天,还剩下一天。因此,如果我的第一年的第 52 周于 12 月 30 日结束,那么我还有 12 月 31 日需要担心。
我可以认为一年有 53 周,第 53 周是 12 月 31 日、1 月 1 日、1 月 2 日、1 月 3 日……或者,更传统地,我认为第一个下一年的一周实际上从 12 月 31 日开始。这就是你的日记本的运作方式。
当然,这意味着明年第52周结束的时间不是现在的12月30日,而是12月29日。
每年它都会向后推迟一天,直到第六年,我们将第 52 周的结束时间向后移动了 6 天(并为了更好地衡量而加入了闰日),因此 2017 年的整个第一周将是包含在 2016 年,那将是愚蠢的。所以 2016 年将有 53 周。
完全相同的逻辑也适用于月份,但可能更难发现。不幸的是,您选择了 2011 年 8 月,该日期的安排很巧妙,即每月 1 日从星期一开始。
8 月 29 日位于 8 月的第 5 周,但按照相同的逻辑,9 月的 1 日、2 日、3 日、4 日也将位于 8 月的第 5 周,因此不能位于 9 月的第一周。
所以 9 月 29 日将是,简单地将天数除以 7 的方法是,在 9 月的第 5 周,但是查看上面的日历,如果 9 月 1-4 日在 8 月,那么 9 月 29 日就是九月的第四周。
这完全取决于您对一周何时开始的定义。
上面,第 0 周表示该周不被视为本月的一部分。所以9月1日是8月的第5周。
注意:
我想对 @unutbu 的答案发表评论,但我猜我没有足够的观点。
模 7 方法在 2011 年 9 月失败。
从上面的日历来看,9 月 1 日在第一周,但这意味着 28 日不能在第 4 周 - 它必须在第 5 周,或者第 1 日在第 0 周......
The modulo-style approach shown in other answers can be misleading. Imagine weeks in a year. THere are 52 chunks of 7 days in any 365 day year, with one day left over. So if for my first year, the 52nd week ends on Dec 30, and I have Dec 31 left to worry about.
I could either consider that there are 53 weeks in the year, and have the 53rd week be 31 Dec, 1 Jan, 2 Jan, 3 Jan ... Or, more conventionally, I consider that the first week of the next year actually begins on Dec 31. This is how your diary pocket book does it.
Of course this means that the next year the 52nd week ends not on Dec 30 now, but Dec 29.
And each year it creeps back one day at a time, until the 6th year where we have moved the end of 52nd week back 6 days (and thrown in a leap day for good measure) so the whole of the 1st week of 2017 would be contained in the year 2016, and that would be silly. So 2016 will have 53 weeks in it.
The exact same logic applies to months, however it can be harder to spot. Unfortunately you choose August 2011 which has a neat arrangement of starting the 1st of the month on a monday.
29 of Aug is in 5th week of Aug, but with same logic, the 1st, 2nd 3rd 4th of Sept would also be in the 5th week of Aug, and so cannot be in 1st week of Sept.
So 29 Sept would be, by the simple divide number of days by 7 approach, be in 5th week of September, but looking at calendar above, if 1-4 of sept are in August, then 29 Sept is in 4th week of Sept.
It all depends on your defintion of when a week begins.
Above, week 0 means the week is not considered part of this month. So 1st sept is in the 5th week of august.
NB
I would like to comment on @unutbu answer but I dont have enough points I guess.
The modulo 7 approach fails in sept 2011.
from the above calendar, 1st of sept is in week one, but that means the 28th cannot be in week 4 - it must be either in week 5, or 1st is in week 0...
也许 http://labix.org/python-dateutil 会有所帮助。
除此之外,这只是数学。
Maybe http://labix.org/python-dateutil will help.
Other than that it's just math.
这个(有点笨拙)的例子,使用日历模块……
根据OP的预期,2011-02-28的答案是错误的,除非每周从星期二开始。
This (somewhat clumsy) example, using the calendar module ...
... gets the answer wrong for 2011-02-28, based on the OP's expectation, unless weeks start on Tuesday.
这个怎么样。我们给出了我们希望查找的星期的日期。我们在新变量中将日期重置为该月的第一天。然后,我们为第一天和给定日期创建一年中的一周。从给定日期的等周中减去第一个等周并添加一个。您添加 1 来纠正零索引号。这应该给出该月的周数。
How about this. We give the date that we wish to find the week for. We reset the date to the first day of the month in a new variable. We then create the week in the year for both the first day and our given date. Subtract the first isoweek from the given date's isoweek and add one. You add one to correct the zero index number. This should give the week number in the month.
一般求除法的上限:(x/y)的上限=(x+y-1)/y
In general to find the ceiling of division: ceiling of (x/y) = (x+y-1)/y
考虑到@Usagi的评论:
以下等式表明一个月的第一天不一定是星期一:
To take into account the comment of @Usagi:
The following equation manage that the first day of a month is not necessary a monday:
在此处检查答案:https://stackoverflow.com/a/64192858/6089311
Check answer here: https://stackoverflow.com/a/64192858/6089311