JavaScript如何判断某年是否为闰年
How to determine whether a year is a leap year in JavaScript
我正在尝试确定某年是否为闰年。我不确定我在哪里遗漏了一些东西,因为这段代码是为了确定这一点。
感谢您的帮助。
let Year = (year) => {
this.year = year;
};
Year.prototype.isLeap = () => {
return (
this.year % 400 === 0 ||
(this.year % 4 === 0 && (this.year % 100 === 0))
);
};
let year = new Year(2014);
year.isLeap();
谢谢,我已经弄明白了。
最初我做的是你们指的那种 If 语句 here!所以我现在正在重构一个更清晰的代码。
我的代码在这一行有问题
(this.year % 4 === 0 && (this.year % 100 === 0))
正确的语法是
(this.year % 4 === 0 && !(this.year % 100 === 0))
您可以只检查给定年份的 2 月 29 日,看看它是否更改为 3 月 1 日。
const date = new Date(this.year, 1, 29);
return date.getMonth() === 1;
如果getMonth()
returns 1,那么现在还是二月,也就是闰年
Number.prototype.isLeap = function() {
return !(this % 4 || !(this % 100) && this % 400);
}
let year = 2000;
console.log(year.isLeap()); // prints true
year = 1900;
console.log(year.isLeap()); // prints false
year = 1904;
console.log(year.isLeap()); // prints true
year = 2003;
console.log(year.isLeap()); // prints false
如果删除 function 关键字,以下代码块将在 Javascript 和 Typescript 上运行良好。要了解此实现背后的逻辑,请查看此 link How to determine whether a year is a leap year。
function isLeapYear(year) {
let isLeapObj = {};
if ((year % 4 === 0 && year % 100 != 0) || year % 400 === 0) {
isLeapObj['isLeap'] = true;
isLeapObj['days'] = 366;
} else {
isLeapObj['isLeap'] = false;
isLeapObj['days'] = 365;
}
return isLeapObj;
}
x = isLeapYear(2020);
console.log(x);
对于 Javscript 使用以下代码
关于@brenjt 的上述回答,您可能希望将值 29 更改为 30
const date = new Date(this.year, 1, 30);
if (date.getMonth() === 1) {
console.log("it's not a leap year");
} else {
console.log("it's a leap year");
}
我正在尝试确定某年是否为闰年。我不确定我在哪里遗漏了一些东西,因为这段代码是为了确定这一点。
感谢您的帮助。
let Year = (year) => {
this.year = year;
};
Year.prototype.isLeap = () => {
return (
this.year % 400 === 0 ||
(this.year % 4 === 0 && (this.year % 100 === 0))
);
};
let year = new Year(2014);
year.isLeap();
谢谢,我已经弄明白了。
最初我做的是你们指的那种 If 语句 here!所以我现在正在重构一个更清晰的代码。
我的代码在这一行有问题
(this.year % 4 === 0 && (this.year % 100 === 0))
正确的语法是
(this.year % 4 === 0 && !(this.year % 100 === 0))
您可以只检查给定年份的 2 月 29 日,看看它是否更改为 3 月 1 日。
const date = new Date(this.year, 1, 29);
return date.getMonth() === 1;
如果getMonth()
returns 1,那么现在还是二月,也就是闰年
Number.prototype.isLeap = function() {
return !(this % 4 || !(this % 100) && this % 400);
}
let year = 2000;
console.log(year.isLeap()); // prints true
year = 1900;
console.log(year.isLeap()); // prints false
year = 1904;
console.log(year.isLeap()); // prints true
year = 2003;
console.log(year.isLeap()); // prints false
如果删除 function 关键字,以下代码块将在 Javascript 和 Typescript 上运行良好。要了解此实现背后的逻辑,请查看此 link How to determine whether a year is a leap year。
function isLeapYear(year) {
let isLeapObj = {};
if ((year % 4 === 0 && year % 100 != 0) || year % 400 === 0) {
isLeapObj['isLeap'] = true;
isLeapObj['days'] = 366;
} else {
isLeapObj['isLeap'] = false;
isLeapObj['days'] = 365;
}
return isLeapObj;
}
x = isLeapYear(2020);
console.log(x);
对于 Javscript 使用以下代码
关于@brenjt 的上述回答,您可能希望将值 29 更改为 30
const date = new Date(this.year, 1, 30);
if (date.getMonth() === 1) {
console.log("it's not a leap year");
} else {
console.log("it's a leap year");
}