利用js 计算当前距离年底还有多少天
主要需要考虑是否为闰年 这是一个关键点
//计算当日距离这个元月1号还有多少天
function figureOutDay(){
var brefor7MonthDays;
var days,restDays,nowDay,flag;
var date=new Date();
nowDay=date.getDate();
days=oneYearDays(date);
flag=judgeYear(date);
var month=date.getMonth()+1;
if(month<=2){
if(month%2==0){
flag==1?restDays=days-31-nowDay:restDays=days-31-nowDay;
}else{
restDays=days-nowDay;
}
}
else if(month<=7){
if(month%2==0){
flag==1?restDays=days-((month/2)*31+(month/2-2)*30+29+nowDay):restDays=days-((month/2)*31+(month/2-2)*30+28+nowDay);
}else{
flag==1?restDays=days-((month-1)/2*31+nowDay+29+(month-(month-1)/2-2)*30):restDays=days-((month-1)/2*31+nowDay+28+(month-(month-1)/2-2)*30);
}
}else{
//先计算前7个月的天数
flag==1?before7MonthDays=4*31+2*30+29:before7MonthDays=4*31+2*30+28;
days=days-before7MonthDays;
month=month-7;
if(month%2==0){
restDays=days-((month/2)*31+(month/2-1)*30+nowDay);
}else{
restDays=days-((month-1)/2*31+(month-1)/2*30+nowDay);
}
}
return restDays;
}
//判断是否为闰年(1表示闰年 0不是)
function judgeYear(date){
var flag;
var year=date.getFullYear();
if(year%4==0){
flag=1;
}else{
flag=0;
}
return flag;
}
//计算一年有多少天(31天 1 3 5 7 8 10 12月,30天4 6 9月,2月判断下(28或者29天))
function oneYearDays(date){
var days;
var year=date.getFullYear;
if(year%4==0){
days=7*31+4*30+29;
}else{
days=7*31+4*30+28;
}
return days;
}