Skip to content Skip to sidebar Skip to footer

Moment JS Subtract Method Returns 12 Hour Difference When There Is Only Zero Hour Left

I want to display remaining hours, minutes and seconds to a specific preset time (01:00 PM) from the current time with help of moment js. I am using the following code, var n = new

Solution 1:

You can't use toLocaleTimeString() because it displays the time of a date, so, as Trunst said in his comment, it will display 12 instead of 0 in case of a 12-hour format.

You can use the format() function of momentjs to force it to 24-hours format:

var eta = a.subtract(b).format("H:mm:ss");

Check a demo there which displays a countdown to your ETA

Note the use of diff instead of substract, see Matt Johnson's comment for the explanations.


Note that if you wan to display an ETA greater than 24h, you will have to use the duration object of momentjs.


Post a Comment for "Moment JS Subtract Method Returns 12 Hour Difference When There Is Only Zero Hour Left"