Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python - How to exclude Sat. and Sun. from weeks

I was hoping that someone could give me some examples of ways in which I can return calendar data that does the following.


  1. Returns the number of days in any given month (past/present/future), but after excluding Saturdays and Sundays.

Example:

input('Year: ') — 2018

input('Month: ') — 7

End Result: The number of weekdays in (July) of (2018) is (22).


  1. Assign an iterator to each weekday after excluding Sat. and Sun.

Example:

input('Year: ') — 2018

input('Month: ') — 7

input('date: ') — 20

End Result: The (20) is a (Friday) and is the (15) weekday of (July), (2018).

This is the code that I've been able to create so far...

import calendar

year = float(input('Year: '))
month = float(input('Month: '))
input_year = []
input_month = []

if year >= 1000 and year <=3000:
    input_year.append(year)
if month >= 1 and month <=12:
    input_month.append(month)

cal_format = calendar.TextCalendar(calendar.MONDAY)
result_cal = cal_format.formatmonth(int(input_year[0]), int(input_month[0]))
print(result_cal)

THE END RESULT IS...

Year: 1978
Month: 3
     March 1978
Mo Tu We Th Fr Sa Su
       1  2  3  4  5
 6  7  8  9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31

This only prints out a Text Calendar with Sat. and Sun at the end of each week, so I can at least visually exclude them. But I would really like to be able to programmatically exclude them and be able to input the above variables to calculate weekdays in a month, and which weekday each day is within that month.

like image 590
Michael Scott Perkins Avatar asked Oct 25 '25 00:10

Michael Scott Perkins


2 Answers

To get the number of weekdays in a month:

import calendar

weekdays = 0
cal = calendar.Calendar()

for week in cal.monthdayscalendar(2018, 7):
    for i, day in enumerate(week):
        # Check if is a weekday and the day is from this month
        if i < 5 and day != 0:
            weekdays += 1

print weekdays

To get the weekday number of a specific day, you can modify the above code to return the weekday count when the input day is reached.

like image 98
erickque Avatar answered Oct 27 '25 16:10

erickque


Here's one way to find the number of previous weekdays:

Note that the type conversions for year, month, day are int.

import calendar

year = int(input('Year: '))
month = int(input('Month: '))
day = int(input('Day: '))

full_wks = day / 7
extra_days = day % 7

first_day = calendar.weekday(year, month, 1)
if first_day >= 5:              # if month begins on a weekend
    weekend = 7 - first_day     # yields 1 if Sunday, 2 if Saturday
    extra_days -= weekend

weekdays = full_wks * 5 + extra_days

ordinal = lambda n: "{}{}".format(n, 'tsnrhtdd'[n%5*(n%100^15>4>n%10)::4])

print "{}/{} is the {} weekday in the month.".format(month, day, ordinal(weekdays))

Output:

Year: 2018
Month: 7
Day: 20
7/20 is the 15th weekday in the month.

Ordinal number conversion from xsot on Code Golf.

like image 43
Ellburrito Avatar answered Oct 27 '25 17:10

Ellburrito



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!