Javascript获得闰年

时间:2015-10-02 01:41:22

标签: javascript

我的代码需要帮助。我需要将我的函数leapYear()调用到我的FieldValidator()函数,以确定用户给出的年份是否是闰年。

这是我的代码:

function FieldValidator() {
     var err = "";
     var valid = false;
     var leap = new leapYear(year)
     //year
     if(document.getElementById('year').value == ""){
        valid = true;
        err += "Enter year \n";
        document.getElementById('year').style.borderColor = "red";
     }
     else if(document.getElementById('year').value < 1000 || document.getElementById('year').value > 9999){
        valid = true;
        err += "Invalid Year \n";
        document.getElementById('year').style.borderColor = "red";
     }
     else {
        document.getElementById('year').style.borderColor = "green";
     }
     //month
     if(document.getElementById('month').value == ""){
        valid = true;
        err += "Enter Month \n";
        document.getElementById('month').style.borderColor = "red";
     }

     else if(document.getElementById('month').value < 1 || document.getElementById('month').value > 12){
        valid = true;
        err += "Invalid Month\n";
        document.getElementById('month').style.borderColor = "red";
     }
     else {
        document.getElementById('month').style.borderColor = "green";
     }
     //day
     if(document.getElementById('day').value == ""){
        valid = true;
        err += "Enter day \n";
        document.getElementById('day').style.borderColor = "red";
     }
     else if (document.getElementById('month').value == 2) {
        if(document.getElementById('year').value == leap()) {
            if(document.getElementById('day').value > 29) {
                valid = true;
                err += "invalid leap\n";
                document.getElementById('day').style.borderColor = "red";
            }
            else {
                document.getElementById('day').style.borderColor = "green";
            }
        }
        else if(document.getElementById('year').value != leap()) {
            if(document.getElementById('day').value > 28) {
                valid = true;
                err += "invalid \n";
                document.getElementById('day').style.borderColor = "red";
            }
            else {
                document.getElementById('day').style.borderColor = "green";
            }
        }
     }
     else if (document.getElementById('month').value != 2) {
        if(document.getElementById('day').value < 1 || document.getElementById('day').value > 31 ) {
            valid = true;
            err += "Invalid day \n";
            document.getElementById('day').style.borderColor = "red";
        }
        else {
        document.getElementById('day').style.borderColor = "green";
        }
     }
     else {
        document.getElementById('day').style.borderColor = "green";
     }

     if(valid){
        alert(err)
        return false;
     }
     return true;
}

function leapYear(year)
{
    return ((document.getElementById('year').value % 4 == 0) && (document.getElementById('year').value % 100 != 0)) || (document.getElementById('year').value % 400 == 0);
}

1 个答案:

答案 0 :(得分:4)

对于任何相关的时间,我正在使用momentjs(在浏览器中或在nodejs中的服务器上)。

就这么简单。

<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.10.6/moment.min.js">
var year = 2015;

moment([year]).isLeapYear(); // false
</script>

isLeapYear函数本身也很简单。因此,您可以使用它来确定年份是否为闰年。

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

Source