如何在 React Native 中的 moment.js 中找到时间之间的差异

问题描述:

如何在 React Native 中找到时差(我使用的是 moment):

How can I find difference time in React Native (I'm using moment):

喜欢:

let end = endTime; // 10:10:05
let start = startTime; // 10:10:03

moment.utc(moment(end," hh:mm:ss").diff(moment(start," hh:mm:ss"))).format("mm:ss")

//expected output: 00:00:02 

你需要使用 moment.duration

You would need to use moment.duration

function timeRemaining (start, end) {
  // get unix seconds
  const began = moment(start).unix();
  const stopped = moment(end).unix();
  // find difference between unix seconds
  const difference = stopped - began;
  // apply to moment.duration
  const duration = moment.duration(difference, 'seconds');
  // then format the duration
  const h = duration.hours().toString();
  const m = duration.minutes().toString().padStart(2, '0');
  const s = duration.seconds().toString().padStart(2, '0');
  return `${h}:${m}:${s}`;
}