Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How can I determine the weekday of a leap day?

I am trying to figure out what day of the week the leap day falls under (ex. Sunday, Monday, etc) for each year (ex. 1972 Tuesday) in a range of years.

The code below checks that each year is a leap year and appends it to an array:

import array as year

LeapYear = []

startYear = 1970
endYear = 1980

for year in range(startYear, endYear):
    if (0 == year % 4) and (0 != year % 100) or (0 == year % 400):
        LeapYear.append(year)
        print(year)

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You are kind of reinventing the wheel. You can use calendar.isleap to determine if a year is a leap year. To get the zero-indexed day of the week (with the week starting on Mondays), you can use calendar.weekday.

import calendar

days = 'Mon Tue Wed Thu Fri Sat Sun'.split()
start = 1994
end = 2025

for year in range(start, end):
    if calendar.isleap(year):
        day_ix = calendar.weekday(year, 2, 29)
        print(year, days[day_ix])

# prints:
1996 Thu
2000 Tue
2004 Sun
2008 Fri
2012 Wed
2016 Mon
2020 Sat
2024 Thu
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading