Home >Web Front-end >Front-end Q&A >How to determine if this year is a leap year in javascript

How to determine if this year is a leap year in javascript

青灯夜游
青灯夜游Original
2022-02-15 17:29:556733browse

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.

How to determine if this year is a leap year in javascript

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+" 不是闰年");
}

How to determine if this year is a leap year in javascript

[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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn