datetime - How to determine whether a year is a leap year in JavaScript - Stack Overflow

I'm trying to determine whether a year is a leap year or not. I'm not sure where i'm mis

I'm trying to determine whether a year is a leap year or not. I'm not sure where i'm missing something because this code is meant to determine that.

Thanks for your help.

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();

Thanks I've figured it out.

Initially i did it will the kind of If statement you guys are pointing to here!, so I'm now refactoring to av a cleaner code.

My code was having issue on this line

(this.year % 4 === 0 && (this.year % 100 === 0))

the right syntax is

(this.year % 4 === 0 && !(this.year % 100 === 0))

I'm trying to determine whether a year is a leap year or not. I'm not sure where i'm missing something because this code is meant to determine that.

Thanks for your help.

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();

Thanks I've figured it out.

Initially i did it will the kind of If statement you guys are pointing to here!, so I'm now refactoring to av a cleaner code.

My code was having issue on this line

(this.year % 4 === 0 && (this.year % 100 === 0))

the right syntax is

(this.year % 4 === 0 && !(this.year % 100 === 0))
Share Improve this question edited May 10, 2018 at 4:13 Mindsworth asked May 10, 2018 at 3:27 MindsworthMindsworth 372 silver badges10 bronze badges 8
  • 1 Use a standard function instead of an arrow function in order to capture the calling context (both in Year and isLeap) – CertainPerformance Commented May 10, 2018 at 3:28
  • 1 Possible duplicate of Writing a JavaScript program to calculate a leap year – Mark Commented May 10, 2018 at 3:29
  • 1 Your logic is wrong anyway ... it thinks that leap years are only 1900,2000,2100, etc – Jaromanda X Commented May 10, 2018 at 3:32
  • this.year % 4 === 0 && (this.year % 100 !==0 || this.year %400 == 0) – Jaromanda X Commented May 10, 2018 at 3:35
  • This question already has an answer here: stackoverflow./questions/16353211/… – hoangkianh Commented May 10, 2018 at 3:37
 |  Show 3 more ments

4 Answers 4

Reset to default 8

You could just check the feburary 29th of the given year and see if its changes to march 1st.

const date = new Date(this.year, 1, 29);
return date.getMonth() === 1;

If getMonth() returns 1, then its still feburary which means its leap year.

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

The following code block will work well on Javascript and also on Typescript if you remove the function keyword. To understand the logic behind this implementation have a look at this 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);

For Javscript use the following code

In regards to @brenjt's answer above you might want to change the value 29 to 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");
}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742369775a4431030.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信