Skip to content

Instantly share code, notes, and snippets.

@19Site
Last active January 22, 2020 11:55
Show Gist options
  • Save 19Site/c915bda58240e5768e405f30849bfbee to your computer and use it in GitHub Desktop.
Save 19Site/c915bda58240e5768e405f30849bfbee to your computer and use it in GitHub Desktop.
Check year is leap year
'use strict';
function isLeapYear(year) {
// result
var result = undefined;
// set as today
if (typeof year === 'undefined') {
year = new Date().getYear();
}
// is date object
else if (typeof year === 'object' && year instanceof Date) {
// get year
year = year.getYear();
}
// year is number
if (typeof year === 'number' && year !== NaN) {
// assign result
result = ((year % 4) === 0 && (year % 100) !== 0) || (year % 400) === 0;
}
// valid result
if (typeof result === 'number') {
// return result
return result;
}
// invalid result
else {
// invalid input value
throw new Error('invalid input value');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment