Using Python to count working days per month?

I am trying to write a Python script that calculates the number of working days in the current month. For example, if month = August , then businessDays = 22 .

Here is my month detection code:

 def numToMonth( num ): months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"] return str(months[ num - 1 ]) 

This code works fine, and I could hard code another function to match the month with how many days this month should contain ... but that does not help me on weekdays.

Any help? I'm used to C, C ++, so please don't bash my Python skills. "

Edit: I cannot install any additional libraries or modules on my computer, so please submit responses using Python default modules. (Python 2.7, datetime , etc.) In addition, Windows 7 is installed on my PC.

+4
source share
6 answers

This is a long way to go, but at least it works and requires nothing but 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 
+9
source

I would just use the calendar built-in module:

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

what he.

+7
source

UPDATE: OP cannot use external libraries. Then you will need to create several tables based on the definition of 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 number of the month in the table of months, y is the last two digits of the year, and c is the number of the century.

This is tiring, but not impossible!

Answer ORIG:. It is very tedious to code yourself, because August 1, 2013 and August 1, 2012 are not necessary on the same day of the week. I would start with the class 'date' 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 the function 'datetime.weekday ()'.

0
source

It is relatively simple, just divide it into steps:

  • You need to go through days a month.
  • You need to determine the day of the week on which a date falls. Wikipedia lists some methods.
  • Then you only need the flag days of the week as working days or not.

Combine these steps and you will have a working method.

0
source

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

  • choose a date on which you know the day of the week - it is better if it's Monday
  • come up with formulas for the number of days that have passed since the time that the first of this month.
  • for each of the days of the month (the number of days from the date of your day)% 7 on [5, 6] is a weekend.
0
source

I would like to add my answer.

I use the calendar, list comprehension, and length to calculate how many days are the working day of 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
source

Source: https://habr.com/ru/post/1497035/


All Articles