js简单小示例
以下代码来自于GPT4o/Claude
function calculateDaysBetweenTimestamps(timestamp1, timestamp2) {
const date1 = new Date(timestamp1);
const date2 = new Date(timestamp2);
// 将日期设置为各自的午夜时间,忽略时分秒
date1.setHours(0, 0, 0, 0);
date2.setHours(0, 0, 0, 0);
// 计算两个日期之间的毫秒差
const differenceInTime = date2.getTime() - date1.getTime();
// 将毫秒差转换为天数
const differenceInDays = Math.round(differenceInTime / (1000 * 3600 * 24));
return differenceInDays;
}
// 使用示例
const today = Math.floor(Date.now() / 1000); // 当前时间戳(秒)
const futureDate = today + 7 * 24 * 60 * 60; // 7天后的时间戳
const daysBetween = calculateDaysBetweenTimestamps(today, futureDate);
console.log(`两个时间戳之间的天数: ${daysBetween}`);