【JS】计算两个日期相隔多少天



/**
 * 计算两个日期相隔多少天,不限制先后顺序
 * @param {Date} Date_start 开始时间
 * @param {Date} Date_end 结束时间
 * @returns 相隔天数
 * 可传一个参数,第二个参数不传为当前日期
 */
const beApartDays = (Date_start, Date_end = new Date()) => {
    
    
    // 时间格式化
    let date1 = new Date(Date_start);
    let date2 = new Date(Date_end);
    date1 = new Date(date1.getFullYear(), date1.getMonth(), date1.getDate());
    date2 = new Date(date2.getFullYear(), date2.getMonth(), date2.getDate());
    //目标时间减去当前时间
    const diff = date1.getTime() - date2.getTime();
    //计算当前时间与结束时间之间相差天数
    return Math.abs(diff) / (24 * 60 * 60 * 1000);
};



更多日期时间处理方式,请点击这里进行查看

猜你喜欢

转载自blog.csdn.net/weixin_44244230/article/details/132086890