subtronic
subtronic

Reputation: 25

Get day of week

Task: need to get day of week from and date on proleptic Gregorian calendar(it can be 2009, 935, 2, 342 BP and any date on time axis).

Can not find a mathematical model anywhere. Maybe someone faced with this problem before?

P.S. Implementation language is not important

Upvotes: 0

Views: 123

Answers (1)

user448810
user448810

Reputation: 17876

If January and February are considered as the last two months of the prior year, the weekday differs from the beginning of March by ⌊(13m-1)/5)⌋ days; adjust for leap years and leap centuries to compute the day of the week:

function weekday(ccyy, mm, dd) # zeller's congruence
    days := ["Sun", "Mon", "Tues", "Wednes", "Thurs", "Fri", "Satur"]
    if mm < 3 then mm, ccyy := mm + 10, ccyy - 1 else mm := mm - 2
    cc, yy := ccyy // 100, ccyy % 100
    day := (dd + (13 * mm - 1) // 5 + yy + yy // 4 + cc // 4 - 2 * cc) % 7
    return days[day] || "day"

This varies slightly for dates prior to adoption of the Gregorian calendar.

Upvotes: 2

Related Questions