function getDays(y, m, d) { //当前日期在本年过了多少天
var sum=0;
switch(m-1) {
case 11:
sum+=30;
case 10:
sum+=31;
case 9:
sum+=30;
case 8:
sum+=31;
case 7:
sum+=31;
case 6:
sum+=30;
case 5:
sum+=31;
case 4:
sum+=30;
case 3:
sum+=31;
case 2:
if(y%4==0&&y%100!=0||y%400==0) {
sum+=29;
} else {
sum+=28;
};
case 1:
sum+=30;
}
sum=sum+d;
return sum;
}
function otherDays(y,m,d) {//判断日期在这一年还剩多少天
var otherDays;
if(y%4==0&&y%100!=0||y%400==0) {
otherDays=366-getDays(y,m,d);
} else {
otherDays=365-getDays(y,m,d);
};
return otherDays;
}
function countDays(y1,m1,d1,y2, m2,d2) {
var days;
var sum=0;
if(y1==y2) {
days=getDays(y1,m1,d1)-getDays(y2,m2,d2)
days>=0?days:-days;
} else if(y1<y2) {
for(i=y1+1; i<y2; i++) {
if(i%4==0&&i%100!=0||i%400==0) {
sum+=366;
} else {
sum+=365
};
}
days=sum+ otherDays(y1,m1,d1)+getDays(y2,m2, d2)
} else {
for(i=y2+1; i<y1; i++) {
if(i%4==0&&i%100!=0||i%400==0) {
sum+=366;
} else {
sum+=365
};
}
days=sum+ getDays(y1,m1,d1)+otherDays(y2,m2, d2);
}
return days;
}
alert(countDays(1998,6,27,2017, 6,2));
添加回答
举报
0/150
提交
取消