How to find difference between time in moment.js in React Native

三世轮回 提交于 2021-02-11 15:12:52

问题


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

Like:

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 

回答1:


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}`;
}


来源:https://stackoverflow.com/questions/64777407/how-to-find-difference-between-time-in-moment-js-in-react-native

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!