两个日期类型相减-成都快上网建站

两个日期类型相减

//在公司中用到根据长时间类型请假的情况,根据时间的开始日期好结束日期来批量生成时间段的请假单;
//-------------日期信息---------------------
wisdom.dateDiff = function(strInterval, dtStart, dtEnd)
{
    if (typeof dtEnd == 'string' )//如果是字符串转换为日期型
    {
        dtEnd = wisdom.stringToDate(dtEnd);
    }
    if(typeof dtStart == 'string')
    {
        dtStart = wisdom.stringToDate(dtStart);
    }
    switch (strInterval) {
    case 's' :return parseInt((dtEnd - dtStart) / 1000);
    case 'n' :return parseInt((dtEnd - dtStart) / 60000);
    case 'h' :return parseInt((dtEnd - dtStart) / 3600000);
    case 'd' :return parseInt((dtEnd - dtStart) / 86400000);
    case 'w' :return parseInt((dtEnd - dtStart) / (86400000 * 7));
    case 'm' :return (dtEnd.getMonth()+1)+((dtEnd.getFullYear()-dtStart.getFullYear())*12) - (dtStart.getMonth()+1);
    case 'y' :return dtEnd.getFullYear() - dtStart.getFullYear();
    }
}
wisdom.stringToDate = function(DateStr, format) {
    if (DateStr instanceof Date) {
        return DateStr;
    }
    var converted = Date.parse(DateStr);
    var myDate = new Date(converted);
    if (isNaN(myDate)) {
        if (DateStr.indexOf(':') > 0 || (!wisdom.isEmpty(format) && format.toLowerCase().indexOf("h") > -1)) {
            var two = DateStr.split(' ');
            if (two.length != 2) {
                two = DateStr.split('T');
            }
            var nowDate = new Date();
            if (two.length == 1) {
                var tempValue = new Array();
                tempValue.push(nowDate.toString("yyyy-MM-dd"));
                tempValue.push(two[0]);
                two = tempValue;
            }
            if (two.length != 2) {
                return nowDate;
            }
            var d = two[0].split('-');
            var t = two[1].replace("Z", "").split(':');
            if (t.length == 3) {
                myDate = new Date(d[0], --d[1], d[2], t[0], t[1], t[2]);
            }
            else if (t.length == 2) {
                myDate = new Date(d[0], --d[1], d[2], t[0], t[1], "00");
            }
            else if (t.length == 1) {
                myDate = new Date(d[0], --d[1], d[2], t[0], "00", "00");
            }
            else if (t.length == 1) {
                myDate = new Date(d[0], --d[1], d[2], "00", "00", "00");
            }
        }
        else {
            var arys = DateStr.split('-');
            switch (arys.length) {
                case 1:
                //导致月份无法选,先注释掉
//                    var reg = /^([1-9]\d*)$/; //校验是否为正整数
//                    if (!reg.test(arys[0])) {
//                        return null;
//                    }
                    if (!wisdom.isEmpty(format)) {
                        var dn = new Date();
                        if (format.indexOf("y") > -1) {
                            myDate = new Date(arys[0], 0, "01");
                        } else if (format.indexOf("M") > -1) {
                            myDate = new Date(dn.getYear(), --arys[0], "01");
                        } else if (format.toLowerCase().indexOf("h") > -1) {
                            myDate = new Date(dn.getYear(), dn.getMonth(), dn.getDate(), arys[0], "00", "00");
                        } else if (format.indexOf("m") > -1) {
                            myDate = new Date(dn.getYear(), dn.getMonth(), dn.getDate(), "00", arys[0], "00");
                        } else if (format.toLowerCase().indexOf("s") > -1) {
                            myDate = new Date(dn.getYear(), dn.getMonth(), dn.getDate(), "00", "00", arys[0]);
                        }
                    }
                    break;
                case 2:
                    if (utils.CheckIsDate(DateStr + "-01")) {
                        myDate = new Date(arys[0], --arys[1], "01");
                    }
                    else {
                        return null;
                    }
                    break;
                default:
                    if (utils.CheckIsDate(DateStr)) {
                        myDate = new Date(arys[0], --arys[1], arys[2]);
                    }
                    else {
                        return null;
                    }
                    break;
            }
            //            myDate = new Date(arys[0], --arys[1], arys[2]);
        }
    }
    return myDate;
}
leavetye请假类型的code值;leavename请假类型的中文名称,starttime请假开始时间,endtime请假结束时间
 function LongLeaveTypeAdd(leavetype,leavename,starttime,endtime)
            {
                 var entity = <%=wgLeave.StrClientOprDetailName %>;
                 var detaillst=<%= WgLeaveDetailDet.StrClientOprEntityList %>;
                 var objlst=detaillst;
                 //算出开始日期与结束日期中间差的天数;
                 var leaveday=wisdom.dateDiff('d',starttime,endtime);
                 if(leaveday!=null&&leaveday>0)
                 {
                   for(var i=0;i                   {
                         var d1=wisdom.stringToDate(starttime);
                         var d2 = d1;
                         d2.setDate(d1.getDate()+(i+1));//相差天数累加获得当前天数
                         var ojb=new Object();
                         ojb.leaveAppType_cnName=leavename;
                         ojb.leaveAppType=leavetype;
                         ojb.appStartDate=d2;
                         ojb.appStartLabel_cnName="全天";
                         ojb.appStartLabel="03";
                         objlst.push(ojb);
                   } 
                 }
                 <%=WgLeaveDetailDet.StrClientRebindData %>(objArrClone(objlst));//重绑grid
             }

文章题目:两个日期类型相减
文章来源:http://kswjz.com/article/gsdoih.html
扫二维码与项目经理沟通

我们在微信上24小时期待你的声音

解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流