4 ms·
Can you provide a link to a method that is faster than the doomsday algorithm? If I know the doomsday for a given year, I can get the day of week for a date in
by oh_sigh 10y ago
Can you provide a link to a method that is faster than the doomsday algorithm? If I know the doomsday for a given year, I can get the day of week for a date in about 3 seconds. I'd love to get that even faster(or have a faster method when I don't know the doomsday)
- pklausler 10y agoMemorize this table of offsets: 0 Oct, Jan 1 May 2 Aug 3 Feb, Mar, Nov 4 Jun 5 Sep, Dec 6 Apr, Jul Precalculate the offset for the year (2017 is 117 + trunc(117/4) == 146 == 6 mod 7). Then the day of the week for, say, today, is 6 (for the year) + 3 (for Feb) + 23 (day of the month) == 32 == 4 mod 7, which is Thursday . Other examples: D-Day, 6 June 1944, was (44+11 == 55 == 6 mod 7 for the year) + 4 (Jun) + 6 == 16 == 2 mod 7 == Tuesday. The only twist is needing to subtract 1 from the result for dates in Jan and Feb on leap years, since those dates precede the leap day.
- pmoriarty 10y agoI don't understand how you got the "6 mod 7" in the two examples above. Everything else makes sense, though. Thanks.