js常用方法-判断两个日期范围相差天数

方法

    /**
     * @param {String} 两个日期相差天数(sDate1, sDate2),时间格式【2020-05-10】
     * @returns {Number}    返回相差天数数值
     */
    static dateDiff(sDate1, sDate2) {
        let aDate = sDate1.split('-');
        let oDate1 = new Date(aDate[0], aDate[1], aDate[2]); // 转换为12-18-2006格式
        aDate = sDate2.split('-');
        let oDate2 = new Date(aDate[0], aDate[1], aDate[2]);
        let iDays = parseInt(Math.abs(oDate1 - oDate2) / 1000 / 60 / 60 / 24); // 把相差的毫秒数转换为天数
        return iDays;
    }

 

使用

// 查询天数限制
let beginTime = this.dataHistory.beginTime.slice(0, 10)
let endTime = this.dataHistory.endTime.slice(0, 10)
if (this.myUtils.dateDiff(beginTime, endTime) > 7) {
    this.myUtils.messageType('warning', '时间范围超出了7天限制');
    return;
}

 

 
posted @ 2021-03-11 09:54  老板来斤代码  阅读(1107)  评论(0)    收藏  举报