繁体   English   中英

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

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

如何在 React Native 中找到时差(我使用的是 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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM