Include preceding zeros in a time value using moment.js

Consider the following options for resolving the issue:
– Solution 2: This might be the solution you need.
– Solution 3: Try using this amusing technique.
– Solution: According to the documentation, here is a functional example.
– Solution 2: Attempt to implement this.
– Solution 3: Utilize this method.
– Solution 1: Implement the usage of something similar to the following.


Solution 1:

One available option is utilizing the plug-in that is formatted in moment-duration.

Utilize the

trim

feature to display units that lack value, as mentioned in the documentation.

To prevent the automatic trimming of leading tokens that have no value, simply specify

{ trim: false }

.

Here a working example:

var d1 = moment.duration({
  seconds: 23,
  minutes: 12,
  hours: 1,
  days: 9
});
var d2 = moment.duration({
  minutes: 1
});
console.log(d1.format('DD:HH:mm:ss'));
console.log(d2.format('DD:HH:mm:ss', { trim: false }));



Solution 2:

try

moment(your_date, "your_date_format").format("DD:HH:mm:ss");


Solution 3:

Use this:

function addZeros(d,h,m,s){
  return  ("0"+d).substr(-2) + ":" +("0"+h).substr(-2) + ":"+("0"+m).substr(-2) + ":" +("0"+s).substr(-2);
}

Frequently Asked Questions