Judgment method: 1. Use the "new Date().getFullYear()" statement to get the current year; 2. Use the "year%4==0&&year 0!=0" statement to determine whether the current year is Leap year. If true is returned, it is a leap year. If false is returned, it is not a leap year.
The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.
javascript determines whether this year is a leap year
First we need to get the current year, we can use the Date object and getFullYear().
var myDate = new Date(); var Year = myDate.getFullYear(); //简写: var Year = new Date().getFullYear();
Then determine whether the obtained year is a leap year.
A leap year is a year that is divisible by 4 but not divisible by 100.
Syntax:
year%4==0&&year%100!=0
If true is returned, it is a leap year, if false is returned, it is not a leap year.
Add an if statement to select output information.
if(year%4==0&&year%100!=0){ console.log(year+" 是闰年"); }else{ console.log(year+" 不是闰年"); }
OK, done!
Complete code:
var year=new Date().getFullYear(); if(year%4==0&&year%100!=0){ console.log(year+" 是闰年"); }else{ console.log(year+" 不是闰年"); }
[Related recommendations: javascript learning tutorial]
The above is the detailed content of How to determine if this year is a leap year in javascript. For more information, please follow other related articles on the PHP Chinese website!