diff --git a/utils.py b/utils.py index 5b95263..cb6c347 100644 --- a/utils.py +++ b/utils.py @@ -222,13 +222,12 @@ def get_date_info(input_date, selected_view): # Business logic, should move above to a separate function if selected_view == 'month': - start = dict([(6, 0), (0, 1), (1, 2), (2, 3), (3, 4), (4, 5), (5, 6)]) - start_date = first_day_of_month - \ - timedelta(days=start[first_day_of_month.weekday()]) + # Step 1: Find the first Sunday before or on the first day of the month + days_to_subtract = (first_day_of_month.weekday() + 1) % 7 + start_date = first_day_of_month - timedelta(days=days_to_subtract) - end = dict([(6, 6), (0, 5), (1, 4), (2, 3), (3, 2), (4, 1), (5, 0)]) - end_date = last_day_of_month + \ - timedelta(days=end[last_day_of_month.weekday()]) + # Step 2: Calculate the last day to display, based on the number of weeks + end_date = start_date + timedelta(days=6 * 7 - 1) return { 'next_date': next_month,