Returns the ISO format of the given number of seconds.
s
with the appropriate values to obtain the appropriate values for hour
, minute
and second
.sign
in a variable to prepend it to the result.Array.prototype.map()
in combination with Math.floor()
and String.prototype.padStart()
to stringify and format each segment.String.prototype.join()
to combine the values into a string.const formatSeconds = s => {
const [hour, minute, second, sign] =
s > 0
? [s / 3600, (s / 60) % 60, s % 60, '']
: [-s / 3600, (-s / 60) % 60, -s % 60, '-'];
return (
sign +
[hour, minute, second]
.map(v => `${Math.floor(v)}`.padStart(2, '0'))
.join(':')
);
};
formatSeconds(200); // '00:03:20'
formatSeconds(-200); // '-00:03:20'
formatSeconds(99999); // '27:46:39'
JavaScript, Date
Returns the human-readable format of the given number of milliseconds.
JavaScript, Date
Checks if the given string is valid in the simplified extended ISO format (ISO 8601).
JavaScript, Date
Converts a date to extended ISO format (ISO 8601), including timezone offset.