js根据日期相减获取天数
文章目录
定义方法
/**
* @param {String} startDate 开始日期
* @param {String} endDdate 结束日期
*/
export const getDateByDays = (startDate, endDdate) => {
let d1 = Date.parse(new Date(startDate));
let d2 = Date.parse(new Date(endDdate));
// 时间戳相减 / 天数
let day = parseInt((d2 - d1) / (1000 * 60 * 60 * 24));
return day
}
使用
let tmpTDiff = getDateByDays('2022-06-06', '2022-06-12')
console.log('时间差:', tmpTDiff); // 6天