function calculateDaysDifference(targetDateString) {
const currentDate = new Date();
const targetDate = new Date(targetDateString);
// 计算毫秒差异
const timeDifference = targetDate - currentDate;
// 计算天数差异,如果结果为负数,则设置为0
const daysDifference = Math.max(Math.floor(timeDifference / (1000 * 60 * 60 * 24)), 0);
// 计算小时差异,如果结果为负数,则设置为0
const hoursDifference = Math.max(Math.floor((timeDifference % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60)), 0);
// 计算分钟差异,如果结果为负数,则设置为0
const minutesDifference = Math.max(Math.floor((timeDifference % (1000 * 60 * 60)) / (1000 * 60)), 0);
return {
days: daysDifference,
hours: hoursDifference,
minutes: minutesDifference
};
}
使用chat GPT 生成一个js 计算间隔天数的方法
于 2023-09-26 19:45:00 首次发布