javascript找到闰年

时间:2011-11-17 22:50:28

标签: javascript date textfield leap-year

当我有一个月的2月份时,如何让下面的代码工作?目前它已经到了那天,然后在到达if之前停下来确定它是否是闰年。

 if (month == 2) {
    if (day == 29) {
        if (year % 4 != 0 || year % 100 == 0 && year % 400 != 0) {
            field.focus();
             field.value = month +'/' +  '';
        }
    }
    else if (day > 28) {
        field.focus();
             field.value = month +'/' +  '';
    }
}

13 个答案:

答案 0 :(得分:105)

Date objects用于日期时间更安全,例如

isLeap = new Date(year, 1, 29).getMonth() == 1

由于人们不断询问这是如何工作的,因此它与JS如何计算年 - 月 - 日的日期值有关(详情here)。基本上,它首先计算该月的第一天,然后为其添加N-1天。因此,当我们在非闰年要求2月29日时,结果将是2月1日+ 28天= 3月1日:

> new Date(2015, 1, 29)
< Sun Mar 01 2015 00:00:00 GMT+0100 (CET)

在闰年,1 + 28 = 2月29日:

> new Date(2016, 1, 29)
< Mon Feb 29 2016 00:00:00 GMT+0100 (CET)

在上面的代码中,我将日期设置为2月29日,看看是否发生了翻转。如果不是(月份仍然是1,即2月),这是闰年,否则是非闰年。

答案 1 :(得分:12)

与使用new Date()相比,这大约快了100倍!

<强>更新

这个最新版本使用了底部3位的位测试(是4的倍数),以及检查年份是16的倍数(二进制的底部4位是15)并且是多个25。

ily = function(y) {return !(y & 3 || !(y % 25) && y & 15);};

http://jsperf.com/ily/15

它比我以前的版本(下面)稍快一点:

ily = function(yr) {return !((yr % 4) || (!(yr % 100) && (yr % 400)));};

http://jsperf.com/ily/7

与broc.seib已经很快的条件运算符版本相比,它的速度提高了5%

速度测试结果:http://jsperf.com/ily/6

预期的逻辑测试结果:

alert(ily(1900)); // false
alert(ily(2000)); // true
alert(ily(2001)); // false
alert(ily(2002)); // false
alert(ily(2003)); // false
alert(ily(2004)); // true
alert(ily(2100)); // false
alert(ily(2400)); // true

答案 2 :(得分:6)

正确而快速:

ily = function(yr) { return (yr%400)?((yr%100)?((yr%4)?false:true):false):true; }

如果你处于循环或计算纳秒,这比通过新的Date()对象运行你的年份快两个数量级。比较此处的效果:http://jsperf.com/ily

答案 3 :(得分:5)

isLeap = !(new Date(year, 1, 29).getMonth()-1)

...减去一个应该比大多数CPU架构上的比较更快。

答案 4 :(得分:2)

闰年更好的历史计算。

下面的代码考虑到侏儒历年在公元前45年引入,并且大多数西方世界在1582CE采用公历,0CE = 1BC。

name='RelayState' value='([^"]+)'
name='SAMLResponse' value='([^"]+)'

英国及其殖民地在1752年采用了格里高利历,所以如果你更加以盎格鲁为中心,这个版本更好(我们假设英国在43CE开始采用罗马征服的朱利安历法)。

isLeap = function(yr) {
  if (yr > 1582) return !((yr % 4) || (!(yr % 100) && (yr % 400)));
  if (yr >= 0) return !(yr % 4);
  if (yr >= -45) return !((yr + 1) % 4);
  return false;
};

答案 5 :(得分:1)

我使用这个是因为我讨厌不得不将1月份的0和2月份称为1。 对我和PHP和可读日期,2月= 2。我知道这并不重要,因为数字永远不会改变,但它只是让我的大脑在不同的代码中保持相同的思维。

var year = 2012;
var isLeap = new Date(year,2,1,-1).getDate()==29;

答案 6 :(得分:1)

您可以轻松地通过.isLeapYear()来调用momentjs

var notLeapYear = moment('2018-02-29')
console.log(notLeapYear.isLeapYear()); // false

var leapYear = moment('2020-02-29')
console.log(leapYear.isLeapYear()); // true
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.21.0/moment.min.js"></script>

答案 7 :(得分:1)

全部一行?

const isLeapYear = (year) => (year % 100 === 0 ? year % 400 === 0 : year % 4 === 0);

console.log(isLeapYear(2016)); // true
console.log(isLeapYear(2000)); // true
console.log(isLeapYear(1700)); // false
console.log(isLeapYear(1800)); // false
console.log(isLeapYear(2020)); // true

答案 8 :(得分:1)

function isLeap(year) {   
    if ( (year % 4 === 0 && year % 100 !== 0) || (year % 4 === 0 && year % 100 === 0 && year % 400 === 0) ) {
        return 'Leap year.'
    } else {
        return 'Not leap year.';
    }
}

答案 9 :(得分:0)

  

伪代码

if year is not divisible by 4 then not leap year
else if year is not divisible by 100 then leap year
else if year is divisible by 400 then leap year
else not leap year
  

JavaScript

function isLeapYear (year) {
    return year % 4 == 0 && ( year % 100 != 0 || year % 400 == 0 )
}

如果year不能被4整除,则使用上述代码可以确保您仅对year做一次检查 只需添加括号,您就可以为year保存2张不能被4整除的支票

答案 10 :(得分:0)

另一种选择是查看该年的日期是否为 2月29日。如果确实有这个日期,那么您就知道这是a年。

ES6

// Months are zero-based integers between 0 and 11, where Febuary = 1
const isLeapYear = year => new Date(year, 1, 29).getDate() === 29;

测试

> isLeapYear(2016);
< true
> isLeapYear(2019);
< false

答案 11 :(得分:0)

function leapYear(year){
    if((year%4==0) && (year%100 !==0) || (year%400==0)){
        return true;
    }
    else{
        return false;
    }
}
var result = leapYear(1700);
console.log(result);

答案 12 :(得分:0)

JavaScript 有望获得一个新的日期/时间 API,它会公开一个新的全局对象 - Temporal。这个全局对象为 JS 开发人员提供了一种更好的方式来处理日期/时间。它目前是一个 stage 3 提案,有望很快投入使用。

时间 api 公开了一个很好的属性来检查闰年 - inLeapYear。如果特定日期是闰年,则返回 true,否则返回 false。下面我们使用 with()plainDateISO 返回的日期转换为我们特定年份的日期:

const isLeap = year => Temporal.now.plainDateISO().with({year}).inLeapYear;
console.log(isLeap(2020)); // true
console.log(isLeap(2000)); // true
console.log(isLeap(1944)); // true

console.log(isLeap(2021)); // false
console.log(isLeap(1999)); // false

如果只想检查当前系统日期时间是否为闰年,可以省略.with()

// true if this year is a leap year, false if it's not a leap year
const isLeap = Temporal.now.plainDateISO().inLeapYear;