JavaScript 开关中的同步条件

The simultaneous criteria in JavaScript switch

除了像“28 或 29 天”这样的写法,我们如何添加闰年检查来切换,以获得 2000 年的 29 天?

var month = "Feb";
var year = 2000;
switch(month) {
    case "Apr": case "Jun": case "Sep": case "Nov":
    console.log("30 days."); break;
case "Feb":
    console.log("28 or 29 days."); break;
default: console.log("31 days.");
}

希望对您有所帮助。代码应该是自我解释的。请告诉我。

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

function getMonthLength(month, year) {
  switch(month) {
    case "Apr": case "Jun": case "Sep": case "Nov":
      return 30;
    case "Feb":
      return isLeapYear(year) ? 29 : 28;
    default:
      return 31;
  }
}

console.log(getMonthLength('Feb', 2000));
console.log(getMonthLength('Jan', 2001));
console.log(getMonthLength('Feb', 2001));
console.log(getMonthLength('Sep', 2001));

知道2020年是闰年 写一个这样的函数

 var findLeap = ()=> {
    var dateYear = new Date().getFullYear();
    var leap = dateYear - 2020
 
    if (leap % 4 == 0){
      console.log('Its another Leap year')
  }else{
      console.log('Its NOT a Leap year')
        }
}