python - Week number of the month

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

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

14条回答
  •  萌比男神i
    2020-12-01 08:48

    I found a quite simple way:

    import datetime
    def week(year, month, day):
        first_week_month = datetime.datetime(year, month, 1).isocalendar()[1]
        if month == 1 and first_week_month > 10:
            first_week_month = 0
        user_date = datetime.datetime(year, month, day).isocalendar()[1]
        if month == 1 and user_date > 10:
            user_date = 0
        return user_date - first_week_month
    

    returns 0 if first week

提交回复
热议问题