Javascript – Check for leap year

Javascript – Check for leap year

// Retuns true if year is a leap year; otherwise false
Date.prototype.isLeapYear =
function(utc) {
var y = utc ? this.getUTCFullYear() : this.getFullYear();
return !(y % 4) && (y % 100) || !(y % 400) ? true : false;
};

To check whether 1981 is a leap year (it is not)

// Create a new date
var d = new Date();


// Set the year
d.setFullYear(1981);

// Output is false
d.isLeapYear();

Most Commented Posts

If you enjoyed this post, please consider to leave a comment or subscribe to the feed and get future articles delivered to your feed reader.

Comments

No comments yet.

Leave a comment

(required)

(required)