Skip to content

Instantly share code, notes, and snippets.

@cem2ran
Last active Aug 29, 2015
Embed
What would you like to do?
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