时间段之间相差的 年月日
{y: 0, m: 2, d: 0}
getDiffYmdBetweenDate(sDate1,sDate2) {
//截止时间 加一天 包含截止时间
var addOneDay = function (time) {
// 自定义时间
var diyDate = new Date(time);
var date = new Date(diyDate.setDate(diyDate.getDate() + 1));
// 拼接日期 yyyy-MM-dd 注意月份要 + 1
var newDate = date.getFullYear() + "-" + ((date.getMonth() + 1) > 9 ? date.getMonth() + 1 : "0" + (date.getMonth() + 1)) + "-" + (date.getDate() > 9 ? date.getDate() : "0" + date.getDate());
return newDate
};
sDate2 = addOneDay(sDate2);
var fixDate = function(sDate){
var aD = sDate.split('-');
for(var i = 0; i < aD.length; i++){
aD[i] = fixZero(parseInt(aD[i]));
}
return aD.join('-');
};
var fixZero = function(n){
return n < 10 ? '0'+n : n;
};
var fixInt = function(a){
for(var i = 0; i < a.length; i++){
a[i] = parseInt(a[i]);
}
return a;
};
var getMonthDays = function(y,m){
var aMonthDays = [0,31,28,31,30,31,30,31,31,30,31,30,31];
if((y%400 == 0) || (y%4==0 && y%100!=0)){
aMonthDays[2] = 29;
}
return aMonthDays[m];
};
var checkDate = function(sDate){
};
var y = 0;
var m = 0;
var d = 0;
var sTmp;
var aTmp;
sDate1 = fixDate(sDate1);
sDate2 = fixDate(sDate2);
if(sDate1 > sDate2){
sTmp = sDate2;
sDate2 = sDate1;
sDate1 = sTmp;
}
var aDate1 = sDate1.split('-');
aDate1 = fixInt(aDate1);
var aDate2 = sDate2.split('-');
aDate2 = fixInt(aDate2);
//计算相差的年份
/*aTmp = [aDate1[0]+1,fixZero(aDate1[1]),fixZero(aDate1[2])];
while(aTmp.join('-') <= sDate2){
y++;
aTmp[0]++;
}*/
y = aDate2[0] - aDate1[0];
if( sDate2.replace(aDate2[0],'') < sDate1.replace(aDate1[0],'')){
y = y - 1;
}
//计算月份
aTmp = [aDate1[0]+y,aDate1[1],fixZero(aDate1[2])];
while(true){
if(aTmp[1] == 12){
aTmp[0]++;
aTmp[1] = 1;
}else{
aTmp[1]++;
}
if(([aTmp[0],fixZero(aTmp[1]),aTmp[2]]).join('-') <= sDate2){
m++;
} else {
break;
}
}
//计算天数
aTmp = [aDate1[0]+y,aDate1[1]+m,aDate1[2]];
if(aTmp[1] > 12){
aTmp[0]++;
aTmp[1] -= 12;
}
while(true){
if(aTmp[2] == getMonthDays(aTmp[0],aTmp[1])){
aTmp[1]++;
aTmp[2] = 1;
} else {
aTmp[2]++;
}
sTmp = ([aTmp[0],fixZero(aTmp[1]),fixZero(aTmp[2])]).join('-');
if(sTmp <= sDate2){
d++;
} else {
break;
}
}
return {y:y,m:m,d:d};
},