Python: Number of the Week in a Month

前端 未结 9 604
庸人自扰
庸人自扰 2020-12-03 18:26

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 wa

9条回答
  •  眼角桃花
    2020-12-03 18:56

    To take into account the comment of @Usagi:

    datetime.datetime(2011, 8, 15) // should return 3
    datetime.datetime(2011, 2, 28) // should return 5
    datetime.datetime(2011, 8, 29) // should return 5
    

    The following equation manage that the first day of a month is not necessary a monday:

    (d.day-d.weekday() -2)//7+2
    

    The -2 and +2 allow to get a week number between 1 and 6

提交回复
热议问题