Date

Date()相关

//获取格式化的日期字符串 

    function getDateString(date) {
        var yyyy = date.getFullYear().toString();
        var mm = (date.getMonth() + 1).toString();
        if (mm.length == 1) mm = '0' + mm;
        var dd = date.getDate().toString();
        if (dd.length == 1) dd = '0' + dd;
        return [yyyy, mm, dd].join('-');
    }

//验证关联发布日期和结束日期

    function validDate(start, end, count) {
        if (start && !end && !count) {    //验证发布日期,纠正并返回
            var sd = $(start).val();
            if (!sd || (sd = new Date(sd)) <= new Date()) {
                var tomorrow = new Date(new Date().getTime() + 1 * 86400000);//明天
                var str = getDateString(tomorrow);
                $(start).val(str);
                sd = new Date(str);//只含日期部分,时间部分为0
            }
            return sd;
        }
        else if (start && end && !count) {  //验证结束日期,并返回
            var sd = validDate(start, null, null);
            var ed = $(end).val();
            if (ed && (ed = new Date(ed)) < sd) { ed = false; }
            return ed;
        }
        else if (!start && !end && count) { //验证发布天数,纠正并返回
            var ct = $(count).val().replace(/D|^0/g, "");
            if (!ct || ct.length == 0 || ct < 1) { ct = 1; }
            $(count).val(ct);
            return ct;
        }
        else if (start && !end && count) {  //计算结束日期,并返回
            var sd = validDate(start, null, null);
            var ct = validDate(null, null, count);
            return new Date(sd.getTime() + (ct - 1) * 86400000);
        }
        else if (start && end && count) {   //三方关联逻辑
            var sd = validDate(start, null, null);
            var ed = validDate(start, end, null);
            if (ed) {
                $(count).val((ed.getTime() - sd.getTime()) / 86400000 + 1);//若结束日期有效,则计算发布天数
            }
            else {
                $(end).val(getDateString(validDate(start, null, count)));//若结束日期无效,则根据发布天数,计算并纠正结束日期
            }
        }
    }

 //验证发布日期       

    $(".startdate").focus(function () { validDate(this, null, null); })

//验证并关联结束日期、发布天数

    $(".enddate,.countdate").focus(function () {
        validDate($(".startdate"), $(".enddate"),  $(".countdate")); });     
原文地址:https://www.cnblogs.com/huhunet/p/7356550.html