如何获取JavaScript中两个时间戳之间的差异?

时间:2018-10-11 15:13:20

标签: javascript

我有两个时间戳记:“ 08:00”和“ 10:40”,我需要获得这两个时间戳记之间的分钟数,在这种情况下为160。 有人可以帮忙吗?

1 个答案:

答案 0 :(得分:0)

这不是确切的时间戳。但是您可以通过以下方式做到这一点:

function getMinutesDelta(time1, time2) {
  const [h1, m1] = time1.split(':');
  const [h2, m2] = time2.split(':');
  const ts1 = new Date().setHours(h1, m1);
  const ts2 = new Date().setHours(h2, m2);
  return Math.abs((ts2 - ts1) / (1000 * 60));
}
相关问题