Last active
August 29, 2015 14:01
-
-
Save cem2ran/c2c4b5f952818c52cbae to your computer and use it in GitHub Desktop.
Get the corresponding day of week for a date. Algorithm provided by Claus Tøndering - http://www.tondering.dk/claus/cal/chrweek.php#calcdow.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Given a Date, Month and Year | |
* Return DAY_OF_WEEK. | |
* | |
* @param {Number} [D] Date in the following format: d | |
* @param {Number} [M] Month in the following format: m | |
* @param {Number} [Y] Year in the following format: yyyy | |
* @param {Boolean} [J=false] Defaults to the Gregorian calendar, when J is omitted or set to false. When set to true, the Day Of Week in the Julian calendar is returned. | |
* @return {String} The Day of Week, UPPERCASED. | |
*/ | |
function DayOfDate(D, M, Y, J) { | |
var L = ~~ ((14 - M) / 12); | |
Y -= L; | |
return "SUN MON TUES WEDNES THURS FRI SATUR".split(" ")[((J ? 5 : (~~(Y / 400) - ~~(Y / 100))) + D + Y + ~~(Y / 4) + ~~(31 * (M + 12 * L - 2) / 12)) % 7] | |
+ "DAY" | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Algorithm used in the first revision was provided by Scott Flansburg, the Human Calculator in his talk - http://youtu.be/hesKQ_y1P7k?t=40m0s
Amazing talk! Unfortunately the single lookup table for months wasn't enough to get the Day of Week for all centuries.
Rev. 2 also provides the Day of Week in the Julian Calendar, not that it's used by that many people, but the functionality was added by no more than a ternary and the substitution of two formulas with the number 5.