js写距离生日还有多少天

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u012421679/article/details/50588259

定义全局变量

var daysTotal = 0;
var birthday ="2000-10-01";

                    daysTotal = 0;
                    var year = date.getFullYear();//当前年份
                    var _month = date.getMonth() + 1;//当前月份
                    var _day =date.getDate();//当前天数
                    var month = parseInt(<span style="font-size: 13.3333px;">birthday</span>.split('-')[1]);//生日月份
                    var day = parseInt(<span style="font-size: 13.3333px;">birthday</span>.split('-')[2]);//生日天数
                    if (month > _month)
                    {
                        for (var j = _month; j < month; j++)
                        {
                            daysTotal = TotalDays(year, j, daysTotal);
                        }
                        daysTotal = daysTotal + day - _day;
                    } else if (month < _month) {
                        for (var k = month; k < _month; k++) {
                            daysTotal = TotalDays(year,k, daysTotal);
                        }
                        var dayFull = TotalDays(year, 0, daysTotal);
                        daysTotal = dayFull - daysTotal + day - _day;
                    } else
                    {
                        if (day >= _day) {
                            daysTotal = day - _day;
                        } else
                        {
                            daysTotal = TotalDays(year, 0, daysTotal) - (_day - day);
                        }
                    }
//JS判断闰年代码
function isLeapYear(Year) {
    if (((Year % 4) == 0) && ((Year % 100) != 0) || ((Year % 400) == 0)) {
        return (true);
    } else { return (false); }
}


function TotalDays(year,month,days)
{
    switch (month) {
        case 1:
        case 3: 
        case 5: 
        case 7: 
        case 8: 
        case 10:
        case 12:
            days += 31;
            break;
        case 4:
        case 6:
        case 9:
        case 11:
            days += 30;
            break;
        case 2:
            if (isLeapYear(year)) {
                days += 29;
            } else {
                days += 28;
            }
            break;
        default:
            if (isLeapYear(year)) {
                days = 366;
            } else {
                days = 365;
            }
            break;
    }
    return days;
}



展开阅读全文

没有更多推荐了,返回首页