// 计算两个时间之间的差值
// 定义两个日期对象
let date1 = new Date("2023-10-01T10:00:00");
let date2 = new Date("2023-10-05T14:30:00");
// 计算时间差(以毫秒为单位)
let timeDifference = date2 - date1;
// 将时间差转换为天、小时、分钟和秒
let days = Math.floor(timeDifference / (1000 * 60 * 60 * 24));
let hours = Math.floor((timeDifference % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
let minutes = Math.floor((timeDifference % (1000 * 60 * 60)) / (1000 * 60));
let seconds = Math.floor((timeDifference % (1000 * 60)) / 1000);
console.log(`时间差为:${days} 天,${hours} 小时,${minutes} 分钟,${seconds} 秒`);
// 解释:
// 1. 使用 `new Date()` 创建两个日期对象。
// 2. 通过减法操作符计算两个日期之间的时间差,结果是以毫秒为单位的数值。
// 3. 使用 `Math.floor()` 函数将时间差转换为天、小时、分钟和秒。
// 4. 最后使用 `console.log()` 输出结果。
上一篇:js 日期加一天
下一篇:js 且
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站