本文介绍了我怎样才能找到两次之间的差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

如何在JavaScript中找到时间差异?

How to find the difference between to times in JavaScript?

这是我提出的伪代码

var firstTime = "20:24:00";
var secondTime = "20:00:52";
console.log(firstTime - secondTime);// 23:08 (23 minutes, 8 seconds)


推荐答案

你可以使用 new Date()。setHours()来制作你拥有的日期然后减去他们,从差异中创建一个新的日期:

You could use new Date().setHours() to make to dates from the time you have and then subtract them, make a new date from the difference:

var firstTime = "20:24:00";
var secondTime = "20:00:52";

//                                 transform variables into parameters
let dateA = new Date().setHours(...(firstTime.split(":")));
let dateB = new Date().setHours(...(secondTime.split(":")));

let diff = new Date(dateA-dateB);

console.log(`Differnce: ${diff.getUTCHours()}:${diff.getUTCMinutes()}:${diff.getUTCSeconds()}`);

这篇关于我怎样才能找到两次之间的差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 18:47