javascript 在javascript中检查年份是否为闰年
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16353211/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Check if year is leap year in javascript
提问by BigBob
function leapYear(year){
var result;
year = parseInt(document.getElementById("isYear").value);
if (years/400){
result = true
}
else if(years/100){
result = false
}
else if(years/4){
result= true
}
else{
result= false
}
return result
}
This is what I have so far (the entry is on a from thus stored in "isYear"), I basically followed this here, so using what I already have, how can I check if the entry is a leap year based on these conditions(note I may have done it wrong when implementing the pseudocode, please correct me if I have) Edit: Note this needs to use an integer not a date function
这是我到目前为止所拥有的(条目来自因此存储在“isYear”中),我在这里基本上遵循了这个,所以使用我已经拥有的,我如何根据这些条件检查条目是否是闰年(注意我在实现伪代码时可能做错了,如果我有请纠正我)编辑:注意这需要使用整数而不是日期函数
回答by MMeersseman
function leapYear(year)
{
return ((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0);
}
回答by Eugen Sunic
The function checks if February has 29 days. If it does, then we have a leap year.
该函数检查二月是否有 29 天。如果是这样,那么我们就有了闰年。
ES5
ES5
function isLeap(year) {
return new Date(year, 1, 29).getDate() === 29;
}
ES6
ES6
let isLeap = year => new Date(year, 1, 29).getDate() === 29;
Result
结果
isLeap(1004) // true
isLeap(1001) // false
回答by Crist Xiaotian Ye
A faster solution is provided by Kevin P. Rice here:https://stackoverflow.com/a/11595914/5535820So here's the code:
Kevin P. Rice 在这里提供了一个更快的解决方案:https: //stackoverflow.com/a/11595914/5535820所以这是代码:
function leapYear(year)
{
return (year & 3) == 0 && ((year % 25) != 0 || (year & 15) == 0);
}
回答by nwinkler
If you're doing this in an Node.js app, you can use the leap-yearpackage:
如果您在 Node.js 应用程序中执行此操作,则可以使用闰年包:
npm install --save leap-year
Then from your app, use the following code to verify whether the provided year or date object is a leap year:
然后在您的应用中,使用以下代码验证提供的年份或日期对象是否为闰年:
var leapYear = require('leap-year');
leapYear(2014);
//=> false
leapYear(2016);
//=> true
Using a library like this has the advantage that you don't have to deal with the dirty details of getting all of the special cases right, since the library takes care of that.
使用这样的库的优点是您不必处理使所有特殊情况正确的肮脏细节,因为库会处理这些。
回答by Brian Hayes
You can use the following code to check if it's a leap year:
您可以使用以下代码检查是否为闰年:
ily = function(yr) {
return (yr % 400) ? ((yr % 100) ? ((yr % 4) ? false : true) : false) : true;
}
回答by SrikanthManian
You can try using JavaScript's Date Object
你可以尝试使用 JavaScript 的 Date 对象
new Date(year,month).getFullYear()%4==0
This will return true or false.
这将返回真或假。
回答by Maruf Sarkar
My Code Is Very Easy To Understand
我的代码很容易理解
var year = 2015;
var LeapYear = year % 4;
if (LeapYear==0) {
alert("This is Leap Year");
} else {
alert("This is not leap year");
}