python - Week number of the month

后端 未结 14 1423
忘掉有多难
忘掉有多难 2020-12-01 08:11

Does python offer a way to easily get the current week of the month (1:4) ?

14条回答
  •  温柔的废话
    2020-12-01 08:28

    Josh's answer has to be tweaked slightly to accomodate the first day falling on a Sunday.

    def get_week_of_month(date):
       first_day = date.replace(day=1)
    
       day_of_month = date.day
    
       if(first_day.weekday() == 6):
           adjusted_dom = (1 + first_day.weekday()) / 7
       else:
           adjusted_dom = day_of_month + first_day.weekday()
    
       return int(ceil(adjusted_dom/7.0))
    

提交回复
热议问题