JS计算EasyUI控件两个时间差的问题
计算两个时间差的问题
function getDateIsMatching(){ var pactbegindate=$("#loanbegindate").datetimebox("getValue"); var pactenddate=$("#loanenddate").datetimebox("getValue"); var loanterm=$("#loanterm").val(); var loantermtype=$("#loantermtype").val(); var loantermtypestr; //将日期字符串分隔为数组,数组元素分别为年.月.日 var date1Str = pactbegindate.split("-"); //根据年 . 月 . 日的值创建Date对象 var date1Obj = new Date(date1Str[0],(date1Str[1]-1),date1Str[2]); //将日期字符串分隔为数组,数组元素分别为年.月.日 var date2Str = pactenddate.split("-"); //根据年 . 月 . 日的值创建Date对象 var date2Obj = new Date(date2Str[0],(date2Str[1]-1),date2Str[2]); var year1 = date1Obj.getFullYear(); var year2 = date2Obj.getFullYear(); //相差的年数 var differYears =year2-year1; var month1 = date1Obj.getMonth(); var month2 = date2Obj.getMonth(); //相差的月数 var differMonths=differYears*12+(month2-month1); var day1=parseInt(date1Str[2]); var day2=parseInt(date2Str[2]); //相差的天数 var differDays=day2-day1; if(null!=loantermtype&&loantermtype==1){ loantermtypestr="年"; if (loanterm!=differYears) { alert("起止日期相差的年数"+differYears+loantermtypestr+",合同期限:"+loanterm+loantermtypestr+",请确认起止日期是否正确!"); return false; } if (month2!=month1) { alert("请检查起止日期的月份是否相等!"); return false; } //大于等于就正确 if (differDays<0) { alert("请检查起止日期的'天数'是否正确!"); return false; } return true; }else if(null!=loantermtype&&loantermtype==2){ loantermtypestr="个月"; if (loanterm!=differMonths) { alert("起止日期相差的月数"+differMonths+loantermtypestr+",合同期限:"+loanterm+loantermtypestr+",请确认起止日期是否正确!"); return false; } //大于等于就正确 if (differDays<0) { alert("请检查起止日期的'天数'是否正确!"); return false; } return true; }else{ var t1 = date1Obj.getTime(); var t2 = date2Obj.getTime(); var dateTime = 1000*60*60*24; //每一天的毫秒数 var minusDays = Math.floor(((t2-t1)/dateTime));//计算出两个日期的天数差 var days = Math.abs(minusDays);//取绝对值 loantermtypestr="天"; if (loanterm!=days) { alert("起止日期相差的天数"+days+loantermtypestr+",合同期限:"+loanterm+loantermtypestr+",请确认起止日期是否正确!"); return false; } return true; } }