Using Python to count the number of business days in a month?

后端 未结 6 879
南旧
南旧 2021-01-05 18:27

I am trying to write a Python script that will calculate how many business days are in the current month. For instance if month = August then businessDays

6条回答
  •  心在旅途
    2021-01-05 18:51

    I would simply use built-in module calendar:

    import calendar
    
    weekday_count = 0
    cal = calendar.Calendar()
    
    for week in cal.monthdayscalendar(2013, 8):
        for i, day in enumerate(week):
            # not this month's day or a weekend
            if day == 0 or i >= 5:
                continue
            # or some other control if desired...
            weekday_count += 1
    
    print weekday_count
    

    that's it.

提交回复
热议问题