python - Week number of the month

后端 未结 14 1425
忘掉有多难
忘掉有多难 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:44

    Move to last day of week in month and divide to 7

    from math import ceil
    
    def week_of_month(dt):
        """ Returns the week of the month for the specified date.
        """
        # weekday from monday == 0 ---> sunday == 6
        last_day_of_week_of_month = dt.day + (7 - (1 + dt.weekday()))
        return int(ceil(last_day_of_week_of_month/7.0))
    

提交回复
热议问题