Skip to content

Instantly share code, notes, and snippets.

@noblethrasher
Last active December 28, 2015 11:19
Show Gist options
  • Save noblethrasher/7492317 to your computer and use it in GitHub Desktop.
Save noblethrasher/7492317 to your computer and use it in GitHub Desktop.
var DAYS = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
function is_leap_year(y)
{
if((y % 4) != 0)
return false;
if((y % 100) == 0 && ((y % 400) != 0))
return false;
return true;
}
function get_day_of_week(m, d, y)
{
m = parseInt(m, 10);
d = parseInt(d, 10);
y = parseInt(y, 10);
if(m > 12 || m < 1)
throw "Month must be between 1 and 12";
if(d > 31 || d < 1)
throw "Day must be between 1 and 31";
if(y < 1900)
throw "Year must be greater than 1900";
if((m == 4 || m == 6 || m == 9 || m == 11) && d > 30)
throw "Day of month must be less than 30 for the months of April, June, September, and November";
if(m == 2)
if(is_yeap_year(y))
if(d > 29)
throw "Day must be less than 29 for a leap-year February";
else
if(d > 28)
throw "Day must be less than 28 for a common-year February";
var year = 1900;
var month = 1;
var days = 0;
while (year < y)
{
if(is_leap_year(year))
days+= 366;
else
days+= 365;
year++;
}
while(month < m)
{
if(month == 4 || month == 6 || month == 9 || month == 11)
days += 30;
else
{
if(month == 2)
{
if(is_leap_year(year))
days += 29;
else
days += 28;
console.log('!!!');
}
else
{
days += 31;
}
}
month++;
}
days += parseInt(d, 10);
return DAYS[(days) % 7];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment