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

后端 未结 6 839
南旧
南旧 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:43

    You could take a look at datetime.datetime.dayofweek() but if you are not allowed to use an external library then you need to:

    1. pick a date that you know the day of week of - best if it is a Monday
    2. come up with a formulae for the number of days since then that the first of a given month is.
    3. for each of the days in the month the (number of days since your day) % 7 in [5, 6] is a weekend.
    0 讨论(0)
  • 2021-01-05 18:48

    This is a long-winded way, but at least it works and doesn't require anything other than the standard modules.

    import datetime
    
    now = datetime.datetime.now()
    holidays = {datetime.date(now.year, 8, 14)} # you can add more here
    businessdays = 0
    for i in range(1, 32):
        try:
            thisdate = datetime.date(now.year, now.month, i)
        except(ValueError):
            break
        if thisdate.weekday() < 5 and thisdate not in holidays: # Monday == 0, Sunday == 6 
            businessdays += 1
    
    print businessdays
    
    0 讨论(0)
  • 2021-01-05 18:50

    This is relatively simple, just break it down into steps:

    1. You need to be able to loop through the days in a month.
    2. You need to be able to determine the day of a week that any given date falls on. Wikipedia lists some methods.
    3. Then you need only flag days of the week as business days or not.

    Combine these steps and you'll have a working method.

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2021-01-05 19:01

    I would like to add my answer.

    I'm using Calendar, list comprehension, and length to count how many days is the working day a particular month.

    Here is my code:

    #!/bin/env python
    
    import calendar
    import datetime
    
    now = datetime.datetime.now()
    
    cal = calendar.Calendar()
    
    working_days = len([x for x in cal.itermonthdays2(now.year, now.month) if x[0] !=0 and x[1] < 5])
    
    print "Total working days this month: " + str(working_days)
    
    0 讨论(0)
  • 2021-01-05 19:06

    UPDATE: OP can't use any external libraries. Then you will have to build some tables based on determining the day of the week from the calendar.

    The formula is d + m + y + y/4 + (c mod 7), where: d is the day of the month, m is the month's number in the months table, y is the last two digits of the year, and c is the century number.

    It's tedious but not impossible!

    ORIG answer: It's quite tedious to code yourself, because August 01, 2013 and August 01, 2012 are not necessarily the same day of the week. I'd start with the 'date' class in python (details here

    from datetime import date
    datetime.date(2002, 3, 11)
    t = d.timetuple()
    for i in t:     
       print i
    

    In particular, check out the 'datetime.weekday()' function.

    0 讨论(0)
提交回复
热议问题