Python:一个月中的周数

Fra*_*sta 8 python numbers

指定日期时:

datetime.datetime(2011, 8, 15)
Run Code Online (Sandbox Code Playgroud)

我如何才能知道这是本月的第三周?

如果我想要这个约会怎么办?

datetime.datetime(2011, 2, 28) //should return 4
datetime.datetime(2011, 8, 29) //should return 5
Run Code Online (Sandbox Code Playgroud)

据我所知,如果给定日期是闰年,只有2月有4周(bissextile)

you*_*hat 13

其他答案中显示的模式式方法可能会产生误导.想象一年中的几周.在365天的任何一年中,这是52天的52天,剩下一天.因此,如果我的第一年,第52周结束于12月30日,我将在12月31日左右担心.

我可以考虑到有53个星期的一年,有第53周是12月31日,1月1日,1月2日,1月3日...或者,更传统,我认为明年的第一周实际开始在12月31日.这是你的日记口袋书的作用.

当然这意味着明年第52周不会在现在12月30日结束,而是在12月29日.而且每年它会一次又一天地回来,直到第6年我们已经将第52周结束了6天(并且在闰日中进行了很好的测量)因此2017年的第一周将包含在2016年,这将是愚蠢的.所以2016年将有53周.

完全相同的逻辑适用于几个月,但它可能更难发现.不幸的是,你选择了2011年8月的星期一,这是一个整齐的安排,从月初开始.

>>> print calendar.month(2011,8)
    August 2011
Mo Tu We Th Fr Sa Su
 1  2  3  4  5  6  7
 8  9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31

>>> print calendar.month(2011,9)
   September 2011
Mo Tu We Th Fr Sa Su
          1  2  3  4
 5  6  7  8  9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30
Run Code Online (Sandbox Code Playgroud)

8月29日是8月的第5周,但同样的逻辑,9月1日,2日3日也将在8月的第5周,因此不能在9月的第1周.

因此,9月29日的简单划分天数将是9月的第5周,但是看上面的日历,如果9月1日是8月,那么9月29日是9月的第4周.

这一切都取决于你何时开始一周的定义.

import datetime
import calendar

# I am assuming that the first week of a month starts with the first monday of a month...
#I *think* my logic is OK - if Monday (0) is the start of the week, then
#any dayof the month minus its own day of week (0,1,2...) must be positive
#if that day is on or after the first monday of the month

def week_of_month(tgtdate):

    days_this_month = calendar.mdays[tgtdate.month]
    for i in range(1, days_this_month):
        d = datetime.date(tgtdate.year, tgtdate.month, i)
        if d.day - d.weekday() > 0:
            startdate = d
            break
    # now we canuse the modulo 7 appraoch
    return (tgtdate - startdate).days //7 + 1

tgtdates = [datetime.date(2011, 8, 29),
            datetime.date(2011, 8, 1)
            ]

for tgtdate in tgtdates:
    print tgtdate,
    print "is in week %s" % week_of_month(tgtdate)

print calendar.month(tgtdate.year,tgtdate.month)


 # 2011-09-29 is in week 4
 # 2011-09-01 is in week 0
 #    September 2011
 # Mo Tu We Th Fr Sa Su
 #           1  2  3  4
 #  5  6  7  8  9 10 11
 # 12 13 14 15 16 17 18
 # 19 20 21 22 23 24 25
 # 26 27 28 29 30

 # 2011-08-29 is in week 5
 # 2011-08-01 is in week 1
 #     August 2011
 # Mo Tu We Th Fr Sa Su
 #  1  2  3  4  5  6  7
 #  8  9 10 11 12 13 14
 # 15 16 17 18 19 20 21
 # 22 23 24 25 26 27 28
 # 29 30 31
Run Code Online (Sandbox Code Playgroud)

以上,第0周意味着本周不被视为本月的一部分.因此,1月1日是在8月的第5周.

NB

我想对@unutbu回答发表评论,但我猜我没有足够的分数.模7方法在2011年9月失败.

>>> d = datetime.date(2011,9,28)
>>> (d.day-1)//7+1
4
>>> d = datetime.date(2011,9,1)
>>> 
>>> (d.day-1)//7+1
1
Run Code Online (Sandbox Code Playgroud)

从上面的日历,9月1日是在第一周,但这意味着第28周不能在第4周 - 它必须是在第5周,或第1是在第0周......


unu*_*tbu 9

In [115]: d=datetime.datetime(2011, 2, 28)

In [116]: (d.day-1)//7+1
Out[116]: 4

In [117]: d=datetime.datetime(2011, 8, 29)

In [118]: (d.day-1)//7+1
Out[118]: 5
Run Code Online (Sandbox Code Playgroud)