jsmreese / moment-duration-format

Format function plugin for the Moment Duration object.
MIT License
969 stars 120 forks source link

Formatting zero-valued duration does not follow format #119

Closed jayArnel closed 6 years ago

jayArnel commented 6 years ago

I have a function that accepts a duration in minutes and converts it to format HH[h]mm[m].

function (minutes) {
    return moment.duration(minutes, "minutes").format("HH[h]mm[m]");
}

Example: 123 => 02h03m

I am using this function as a render function for some DataTables columns that receive the data as integer but I need it to be formatted as above.

When the function was passed with 0 minutes, it returned only 00m. I was expecting it to be: 00h00m because I need them all to be in the same format. Shouldn't it follow the same format, regardless? Or am I missing something here?

jsmreese commented 6 years ago

You'll want to set the trim option.

The default behaviour trims leading zero-value tokens:

moment.duration(65, "minutes").format("HH[h]mm[m]");
"01h05m"
moment.duration(5, "minutes").format("HH[h]mm[m]");
"05m"
moment.duration(0, "minutes").format("HH[h]mm[m]");
"00m"

But setting trim: false:


moment.duration(5, "minutes").format("HH[h]mm[m]", { trim: false });
"00h05m"
moment.duration(0, "minutes").format("HH[h]mm[m]", { trim: false });
"00h00m"