Python: Number of the Week in a Month

前端 未结 9 639
庸人自扰
庸人自扰 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:53

    Maybe http://labix.org/python-dateutil will help.

    Other than that it's just math.

     from datetime import datetime, timedelta
    
        def week_of_month(date):
            month = date.month
            week = 0
            while date.month == month:
                week += 1
                date -= timedelta(days=7)
    
            return week
    

提交回复
热议问题