Formatting MomentJS duration above 24 hours

Viewed 11553

I would like to format a summed up total working hours e.g. 49.75 to this: 49:45.

When I use duration like this:

const dur = moment.duration(49.75, 'hours').asMilliseconds();
moment.utc(dur).format("HH:mm:ss") // 01:45:00

I'll receive 01:45:00 instead of 49:45:00

Is there a way to format (instead of HH) duration without dropping the days?

3 Answers

If you are modifying one second at a time, it can be simplified quite a lot (parsing and updating).

Inspiration lifted from this answer: https://stackoverflow.com/a/45629433/11121690

incrementTime: (sectionKey, itemKey) => {
    const pad = (number) => `${number > 9 ? number : "0" + number}`;
    const timeString = STATS_DATA_UTIL.getByParts(sectionKey, itemKey);
    const parts = timeString.split(':');
    let uiSecs = parseInt(parts[2]);
    let uiMins = parseInt(parts[1]);
    let uiHrs = parseInt(parts[0]);

    uiSecs++;
    if (uiSecs === 60) {
        uiSecs = 0; uiMins++;
        if (uiMins === 60) uiMins = 0; uiHrs++;
    }

    const result = `${pad(uiHrs)}:${pad(uiMins)}:${pad(uiSecs)}`;
    STATS_DATA_UTIL.setByParts(sectionKey, itemKey, result);
},
Related