比较年月日
思路:转为毫秒时间计算
function compareDate(that, date1, date2) {
var oDate1 = new Date(date1);
var oDate2 = new Date(date2);
var data1 = Date.parse(date1.replace(/-/g, "/"));
var data2 = Date.parse(date2.replace(/-/g, "/"));
var datadiff = data2 - data1;
var time = 31 * 24 * 60 * 60 * 1000;
if (oDate1.getTime() > oDate2.getTime()) {//转换成毫秒进行比较
that.timeFlag = false;
alert('开始时间不能大于结束时间');
that.currentDate = new Date();
return false;
} else {
if (datadiff < 0 || datadiff > time) {
that.timeFlag = false;
that.currentDate = new Date();
alert("开始时间与结束时间间隔不能超过31天!");
return false;
} else {
that.timeFlag = true;
}
}
}
比较年月
//比较年月
function compareMon(that, date1, date2) {
var arr1 = date1.split('-');
var arr2 = date2.split('-');
var data1 = Date.parse(date1.replace(/-/g, "/"));
var data2 = Date.parse(date2.replace(/-/g, "/"));
var datadiff = data2 - data1;
var time = 12;
var diffY = Math.abs(arr1[0] - arr2[0]);
var flag = true;
if (arr1[0] == arr2[0]) {//同年
if (arr2[1] > arr1[1]) {
if (arr2[1] - arr1[1] > 12) { //月间隔超过12个月
that.timeFlag = false;
that.currentDate = new Date();
alert('月选择间隔不能超过 12 个月');
return false;
}else {
that.timeFlag = true;
}
}else if(arr2[1]==arr1[1]){
that.timeFlag = false;
that.currentDate = new Date();
alert('同年开始月份不能等于结束月份');
return false;
}else{
that.timeFlag = false;
that.currentDate = new Date();
alert('同年开始月份不能大于结束月份');
return false;
}
} else if (arr2[0] > arr1[0]) { //不同年
if (arr2[0] - arr1[0] > 1) {
that.timeFlag = false;
that.currentDate = new Date();
alert('月选择间隔不能超过 12 个月');
return false;
} else if (arr2[0] - arr1[0] == 1) {
if(13 - Math.abs(arr1[1]) + Math.abs(arr2[1]) > 12){
that.timeFlag = false;
that.currentDate = new Date();
alert('月选择间隔不能超过 12 个月');
return false;
}else if (arr1[1] + 12 - arr2[1] < 12) { //月相隔大于12个月
that.timeFlag = false;
that.currentDate = new Date();
alert('月选择间隔不能超过 12 个月');
return false;
}else {
that.timeFlag = true;
}
} else if(arr2[0] - arr1[0] == 0){
that.timeFlag = false;
that.currentDate = new Date();
alert('月选择间隔不能超过 12 个月');
return false;
}
} else {
that.timeFlag = false;
that.currentDate = new Date();
alert('开始年份不能大于结束年份');
return false;
}
}