在Vue3中计算2个时间相差的秒数可以使用Date对象的getTime()方法,将时间转换为毫秒数,然后计算差值并转换为秒数。

以下是一个示例代码:

const startTime = new Date('2021-01-01 00:00:00');
const endTime = new Date('2021-01-01 00:00:30');

const diffInSeconds = Math.floor((endTime.getTime() - startTime.getTime()) / 1000);

console.log(diffInSeconds); // 输出 30

在上面的代码中,我们首先创建了开始时间和结束时间的Date对象,然后使用getTime()方法获取它们的毫秒数,并计算它们的差值。最后,使用Math.floor()方法将差值转换为整数秒数,并将其输出到控制台。

注意,上述代码中使用的日期格式是 yyyy-mm-dd hh:mm:ss,如果你使用其他格式,需要相应地修改代码。

vue3 计算2个时间相差的秒数

原文地址: http://www.cveoy.top/t/topic/fVg 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录