node.js - Wrong hour difference between 2 timestamps (hh:mm:ss) -


using moment.js, want time difference between 2 timestamps.

doing following,

var prevtime = moment('23:01:53', "hh:mm:ss"); var nexttime = moment('23:01:56', "hh:mm:ss");  var duration = moment(nexttime.diff(prevtime)).format("hh:mm:ss"); 

i result :

01:00:03 

why have 1 hour difference? seconds , minutes seem work well.

after doing that, tried following :

function time_diff(t1, t2) {     var parts = t1.split(':');     var d1 = new date(0, 0, 0, parts[0], parts[1], parts[2]);     parts = t2.split(':');     var d2 = new date(new date(0, 0, 0, parts[0], parts[1], parts[2]) - d1);     return (d2.gethours() + ':' + d2.getminutes() + ':' + d2.getseconds()); }  var diff = time_diff('23:01:53','23:01:56'); 

output : 1:0:3

the problem having here when putting nexttime.diff() in moment constructor, feeding milliseconds moment() , tries interpret timestamp, why don't expected result.

there no "nice way" of getting result want apart getting time , manually reconstructing looking :

var dur = moment.duration(nexttime.diff(prevtime));  var formattedduration = dur.get("hours") +":"+ dur.get("minutes") +":"+ dur.get("seconds"); 

and more elegant version give 0 padding in output :

var difference = nexttime.diff(prevtime); var dur = moment.duration(difference); var zeropaddedduration = math.floor(dur.ashours()) + moment.utc(difference).format(":mm:ss"); 

should make happier!

edit : have noticed use hh:mm:ss format, should instead use hh:mm:ss. 'ss' give fractional values seconds between 0 , 999, not want here.


Comments

Popular posts from this blog

scala - 'wrong top statement declaration' when using slick in IntelliJ -

c# - DevExpress.Wpf.Grid.InfiniteGridSizeException was unhandled -

PySide and Qt Properties: Connecting signals from Python to QML -