Skip to content

Instantly share code, notes, and snippets.

@cem2ran
Last active August 29, 2015 14:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cem2ran/c2c4b5f952818c52cbae to your computer and use it in GitHub Desktop.
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.
/*
* 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"
};
@cem2ran
Copy link
Author

cem2ran commented May 7, 2014

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment