javascript - moment.js diff showing wrong result -


i working on small script using momentjs, shows me how many hours , minutes (seperate) have until specific hour. reason result wrong.

thats code far:

var timea = moment('08:00:00', 'hh:mm:ss'); var timeb = moment('16:00:00', 'hh:mm:ss');  var diffab = moment(timea.diff(timeb)).format('hh:mm:ss');  var diffhours = moment(diffab, 'hh:mm:ss').format('h'); var diffminutes = moment(diffab, 'hh:mm:ss').format('mm');  console.log('timea: ' + moment(timea).format('hh:mm:ss')); console.log('timeb: ' + moment(timeb).format('hh:mm:ss'));  console.log('difference a-b: ' + diffab);  console.log('diff hours: ' + diffhours); console.log('diff minutes: ' + diffminutes);   

and thats output:

timea: 08:00:00 timeb: 16:00:00 difference a-b: 17:00:00 diff hours: 17 diff minutes: 00   

the difference of a - b should 8 showing 17.

as per momentjs documentation

if moment earlier moment passing moment.fn.diff, return value negative.

to translate example:

if timea earlier timeb passed moment.diff value negative

and since it's returned milliseconds -28800000 instruction like: take amount of miliseconds midnight, gives different time

so should doing

var diffab = moment(timeb.diff(timea)).utcoffset(0).format('hh:mm:ss'); 

diff documentation https://momentjs.com/docs/#/displaying/difference/

example doc

an easy way think of replacing .diff( minus operator.

          // < b a.diff(b) // - b < 0 b.diff(a) // b - > 0 

Comments

Popular posts from this blog

c# - Update a combobox from a presenter (MVP) -

How to understand 2 main() functions after using uftrace to profile the C++ program? -

How to put a lock and transaction on table using spring 4 or above using jdbcTemplate and annotations like @Transactional? -