I do not usually use JavaScript, and cannot see why this is not working. Thanks.
function testYear() {
var dObject = new Date();
var dRegEx = /^[0-9][0-9]00$/;
var d = dObject.getFullYear();
if (d.match(dRegEx) && d % 400 == 0) {
alert("The year "+d+" is in fact a leap year!");
//return true;
}
else if(!(d.match(dRegEx) && d % 400) && d % 4 == 0) {
alert("The year "+d+" is in fact a leap year!");
//return true;
}
else {
alert("The year "+d+" is not a leap year.");
//return false;
}
}
There’s no need to use a regular expression here. Just use an ordinary integer arithmetic to see if the year is divisible by 100:
By the way, if
d % 400 === 0then it is automatically true thatd % 100 === 0so the extra test is unnecessary.A year is a leap year in the Gregorian calendar either if it is divisible by 400 or if it is divisible by 4, but not by 100. Try this instead: