问题
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