【无标题】js计算两个日期相差多少天数

这篇博客详细介绍了如何使用JavaScript计算两个日期之间的天数差。通过`getStartEnd`、`getMonthEndDate`和`isLeapYear`等辅助函数,实现了不同情况下的日期差计算,包括同月、不同月以及不同年的情况。此外,还提供了处理平年和闰年天数差异的`getDayNum`函数,确保了计算的准确性。
摘要由CSDN通过智能技术生成

这里写自定义目录标题

js计算两个日期相差多少天数

//计算开始天数差
getStartEnd(year, month, date){
let number = 0;
console.log(parseInt(date),“parseInt(date)”)
let start_day = parseInt(date);
console.log(this.getMonthEndDate(year,month),“this.getMonthEndDate(year,month-1)”)
let end_day = this.getMonthEndDate(year,month)
return Math.abs(parseInt(end_day) - parseInt(start_day) + 1)
},
//获取当月最后一天
getMonthEndDate(year,month) {
var new_year = year; //取当前的年份
var new_month = month++;//取下一个月的第一天,方便计算(最后一天不固定)
if(month>12) {
new_month -=12; //月份减
new_year++; //年份增
}
var new_date = new Date(new_year,new_month,1); //取当年当月中的第一天
return (new Date(new_date.getTime()-10006060*24)).getDate();//获取当月最后一天日期
},
// 判断某一年是平年还是闰年
isLeapYear(year){
let number = 0;
((year%40) && (year%100!=0)) || (year%4000) ? number = 366 : number = 365;
// console.log(number)
return number;
},

//获取当两个年份差为1的天数
getBetweenYears(_year1,_year2,month1,month2,date1,date2){
    // 第一个日期
    let monthRange1 = [];
    let month1_mun1 = 0;
    let other_num1 = 0;
    
    let len1 = 12 - parseInt(month1);
    if(len1 > 0){//若果月份之差大于0
        for(var j = 1; j <= len1; j ++){
            let _m1 = this.getMonthEndDate(_year1,Math.abs((parseInt(month1)+j)))

            monthRange1.push(parseInt(_m1))
        }
        other_num1 = monthRange1.reduce((sum,item) =>{//前面其他部分
            return sum + item;
        },0);
        month1_mun1 = this.getStartEnd(_year1,month1,date1);//前面第一部分
        
    }else{//如果当前月为12月
        month1_mun1 = this.getStartEnd(_year1,month1,date1);//前面第一部分
    }
    // 第二个日期
    let monthRange2 = [];
    let month2_mun1 = 0;
    let other_num2 = 0;
    
    let len2 = parseInt(month2) - 1;
    if(len2 > 0){//若果月份之差大于0
        for(var k = 1; k <= len2;k ++){
            let _m2 = this.getMonthEndDate(_year2,Math.abs((parseInt(month2)-k)))

            monthRange2.push(parseInt(_m2))
        }
        other_num2 = monthRange2.reduce((sum,item) =>{//前面其他部分
            return sum + item;
        },0);
        month2_mun1 = parseInt(date2);//前面第一部分
        
    }else{//如果当前月为1月
        month2_mun1 = parseInt(date2);//前面第一部分
    }
     return  parseInt(month1_mun1) + parseInt(other_num1) + parseInt(month2_mun1) + parseInt(other_num2);
},
//计算天数(判断平年,闰年的天数)
getDayNum(day1, day2){
    let _num;//天数
    // console.log(day1, day2)
    let startArr = day1.split('-'),
     endArr = day2.split('-'),
     _year1 = startArr[0],
     _year2 = endArr[0],
     month1 = startArr[1],
     month2 = endArr[1],
     date1 = startArr[2],
     date2 = endArr[2];

    if(_year1 == _year2){//同一年
        if(month1 == month2){//同年同月
            // console.log(month1)
            _num = Math.abs(parseInt(startArr[2]) - parseInt(endArr[2])) + 1;
            // console.log("同年同月",_num)
        }else{//同年不同月
            //计算month1和month2之间的月份
            let monthRange = [];
            let len = parseInt(month2) - parseInt(month1);
            // console.log(len)
            if(len > 1){//如果两个月相减大于1,统计中间的月份
                for(var i = 1; i < len; i++){
                    let _m = this.getMonthEndDate(_year1,Math.abs((parseInt(month1)+i)))
                    monthRange.push(parseInt(_m))
                }
                let other_num = monthRange.reduce((sum,item) =>{
                    return sum + item;
                },0);
                let month1_mun = this.getStartEnd(_year1,month1,date1);
                let month2_mun = parseInt(date2);
                _num = parseInt(month1_mun) + parseInt(month2_mun) + parseInt(other_num);
                // console.log("同年不同月且月份相差大于1",_num)
                
            }else{//如果两个月相减为1
                let month1_mun = this.getStartEnd(_year1,month1,date1);
                let month2_mun = parseInt(date2);
                console.log(parseInt(month1_mun),parseInt(month2_mun),"哈哈哈哈")
                _num = parseInt(month1_mun) + parseInt(month2_mun);
                // console.log("同年不同月且月份相差等于1",_num)
            }
        }
    }else{//不是同一年
        let yearRange = [];
        let other_year_num = 0;
        let _len = _year2 - _year1;
        if(_len > 1){
            for(var h = 1; h < _len; h++){
                let _y = parseInt(_year1 + h);
                
                yearRange.push(this.isLeapYear(_y))
            }
            other_year_num = yearRange.reduce((sum,item) =>{
                return sum + item;
            },0);
            let other_month_num = this.getBetweenYears(_year1,_year2,month1,month2,date1,date2);
            _num = parseInt(other_year_num) + parseInt(other_month_num);
            // console.log("不同年且年份相减大于1",_num)
        }else{
            _num = this.getBetweenYears(_year1,_year2,month1,month2,date1,date2);
            // console.log("不同年且年份相减等于1",_num)
        }
        
    }

    return _num;
},
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值