JSFiddle - React, Tailwind, and code Playground
by aldis
HTML
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.0/moment.min.js"></script>
<div id="duration">
</div>
JavaScript
this.moment.formatDuration = function (duration, timeFormat) {
const ms = duration.asMilliseconds(),
days = Math.floor(Math.abs(ms) / 8.64e7),
msOnLastDay = Math.abs(ms) - days * 8.64e7;
return (ms < 0 ? '-' : '') + (days !== 0 ? days + ' ' : '')
+ moment.utc(msOnLastDay).format(timeFormat ? timeFormat : 'HH:mm:ss.SSS');
};
const elem = $('#duration');
elem.append(moment.formatDuration(moment.duration(10000)) + '<br>');
elem.append(moment.formatDuration(moment.duration(100000000)) + '<br>');
elem.append(moment.formatDuration(moment.duration(-10000)) + '<br>');
elem.append(moment.formatDuration(moment.duration(-100000000)) + '<br>');
elem.append(moment.formatDuration(moment.duration(10000), 'HH:mm') + '<br>');
elem.append(moment.formatDuration(moment.duration(100000000), 'HH:mm') + '<br>');
elem.append(moment.formatDuration(moment.duration(-10000), 'HH:mm') + '<br>');
elem.append(moment.formatDuration(moment.duration(-100000000), 'HH:mm') + '<br>');