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

青灯夜游
Release: 2022-02-15 17:29:55
Original
6692 people have browsed it

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();
Copy after login

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
Copy after login

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

OK, done!

Complete code:

var year=new Date().getFullYear();
if(year%4==0&&year%100!=0){
   console.log(year+" 是闰年");
}else{
   console.log(year+" 不是闰年");
}
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template