pythondatetimerrule

Count weekdays between two dates and exclude dates from list


This code count the weekdays between two dates.

from dateutil.rrule import *
number_weekdays = rrule(WEEKLY, byweekday=(SU,MO,TU,WE,TH),
                        dtstart=datetime.datetime(2021, 5, 1),
                        until=datetime.datetime(2021, 6, 30)).count()

what should I do in order to exclude dates from a list ?

I know about 'exdate' , but I dont know how to use it in this code


Solution

  • import datetime 
    from dateutil.rrule import *
    rules = rruleset()
    
    # all days to count
    rules.rrule(rrule(WEEKLY, byweekday=(SU,MO,TU,WE,TH,FR,SA),
                            dtstart=datetime.datetime(2021, 5, 1),
                            until=datetime.datetime(2021, 6, 30)))
    
    # exlude days from list
    rules.exrule(number_weekdays)
    
    rules.count()