3 ms·
Here's some code I wrote years ago using a different algorithm called Zeller's Congruence[1]. The advantage of this is it doesn't require a lookup table: pub
by chris_overseas 11y ago
Here's some code I wrote years ago using a different algorithm called Zeller's Congruence[1]. The advantage of this is it doesn't require a lookup table:
public int dayOfWeek(int year, int month, int day) {
day += month < 3 ? year-- : year - 2;
return ((((23 * month) / 9) + day + 4 + (year / 4)) - (year / 100) + (year / 400)) % 7;
}
[1] https://en.wikipedia.org/wiki/Zeller%27s_congruence https://en.wikipedia.org/wiki/Zeller%27s_congruence
- imaginenore 11y agoI'm pretty sure the post's solution is wrong. Examples: 2008-06-11 (Wednesday). Post's function returns 2 instead of 3.
- pveierland 11y agoWhen inputting day of month in range [1,31] and month in range [1,12] the function seems to work fine. Verified here from 1900-01-01 through 2020-01-01: http://ideone.com/te3ywm http://ideone.com/te3ywm