Validate number of days in a given month

前端 未结 13 2138
独厮守ぢ
独厮守ぢ 2021-01-31 18:14

Performance is of the utmost importance on this one guys... This thing needs to be lightning fast!


How would you validate the number of days in a given mont

13条回答
  •  星月不相逢
    2021-01-31 18:39

    function daysInMonth(m, y) { // m is 0 indexed: 0-11
        switch (m) {
            case 1 :
                return (y % 4 == 0 && y % 100) || y % 400 == 0 ? 29 : 28;
            case 8 : case 3 : case 5 : case 10 :
                return 30;
            default :
                return 31
        }
    }
    
    function isValid(d, m, y) {
        return m >= 0 && m < 12 && d > 0 && d <= daysInMonth(m, y);
    }
    

提交回复
热议问题