Skip to content

Instantly share code, notes, and snippets.

@SteGriff
Created January 2, 2025 16:30
Show Gist options
  • Save SteGriff/b94c43158002ca82205a29e8fce2fc70 to your computer and use it in GitHub Desktop.
Save SteGriff/b94c43158002ca82205a29e8fce2fc70 to your computer and use it in GitHub Desktop.
A Ruby function to get day of week (Gregorian)
def day_of_week(day, month, year)
# Zeller's Rule - Jan/Feb are Month 13/14 of previous year
if month < 3
month += 12
year -= 1
end
k = year % 100
j = year / 100
# Zeller's congruence
h = (day + (13 * (month + 1)) / 5 + k + (k / 4) + (j / 4) - (2 * j)) % 7
(h + 6) % 7
end
# 0 = Sun, 6 = Sat
puts day_of_week(20, 7, 1969) # Sun
puts day_of_week(5, 1, 2026) # Mon
puts day_of_week(2, 6, 2020) # Tues
puts day_of_week(1, 1, 2025) # Weds
puts day_of_week(4, 7, 1776) # Thurs
puts day_of_week(15, 10, 1582) # Fri
puts day_of_week(3, 4, 1993) # Sat
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment