From 82770125cf590b9ef9b249cdf6ba8c75324c2ad2 Mon Sep 17 00:00:00 2001 From: shanyuhai123 <864299347@qq.com> Date: Sun, 9 May 2021 17:44:55 +0800 Subject: [PATCH 1/3] Add snippet for formatSeconds --- snippets/formatSeconds.md | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 snippets/formatSeconds.md diff --git a/snippets/formatSeconds.md b/snippets/formatSeconds.md new file mode 100644 index 000000000..e7d8776a1 --- /dev/null +++ b/snippets/formatSeconds.md @@ -0,0 +1,26 @@ +--- +title: formatSeconds +tags: date,math,string +--- + +Returns the ISO format of the given number of seconds. + +- Divide `s` with the appropriate values to obtain the appropriate values for "HH:mm:ss". +- Use `Array.prototype.map()` to create the string for each value, and pads it to `2` digits. +- Use `String.prototype.join(', ')` to combine the values into a string. + +```js +const formatSeconds = s => { + if (s < 0) s = 0; + + return [s / 3600, s / 60 % 60, s % 60] + .map(v => `${Math.floor(v).toString().padStart(2, '0')}`) + .join(':'); +}; +``` + +```js +formatSeconds(-200); // "00:00:00" +formatSeconds(200); // "00:03:20" +formatSeconds(99999); // "27:46:39" +``` From 53175da13f34c368117e18aff70c8a2f77bfbb88 Mon Sep 17 00:00:00 2001 From: shanyuhai123 <864299347@qq.com> Date: Sun, 9 May 2021 17:51:32 +0800 Subject: [PATCH 2/3] Update formatSeconds.md fix spaces --- snippets/formatSeconds.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/snippets/formatSeconds.md b/snippets/formatSeconds.md index e7d8776a1..7766c5b32 100644 --- a/snippets/formatSeconds.md +++ b/snippets/formatSeconds.md @@ -14,8 +14,8 @@ const formatSeconds = s => { if (s < 0) s = 0; return [s / 3600, s / 60 % 60, s % 60] - .map(v => `${Math.floor(v).toString().padStart(2, '0')}`) - .join(':'); + .map(v => `${Math.floor(v).toString().padStart(2, '0')}`) + .join(':'); }; ``` From c2536597d09e7a3b2fb94d4de0d28fb7b9a1def6 Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Mon, 17 May 2021 08:59:17 +0300 Subject: [PATCH 3/3] Update formatSeconds.md --- snippets/formatSeconds.md | 31 +++++++++++++++++++------------ 1 file changed, 19 insertions(+), 12 deletions(-) diff --git a/snippets/formatSeconds.md b/snippets/formatSeconds.md index 7766c5b32..d66b5a4b1 100644 --- a/snippets/formatSeconds.md +++ b/snippets/formatSeconds.md @@ -1,26 +1,33 @@ --- title: formatSeconds -tags: date,math,string +tags: date,math,string,intermediate --- Returns the ISO format of the given number of seconds. -- Divide `s` with the appropriate values to obtain the appropriate values for "HH:mm:ss". -- Use `Array.prototype.map()` to create the string for each value, and pads it to `2` digits. -- Use `String.prototype.join(', ')` to combine the values into a string. +- Divide `s` with the appropriate values to obtain the appropriate values for `hour`, `minute` and `second`. +- Store the `sign` in a variable to prepend it to the result. +- Use `Array.prototype.map()` in combination with `Array.prototype.floor()` and `String.prototype.padStart()` to stringify and format each segment. +- Use `String.prototype.join(':')` to combine the values into a string. ```js const formatSeconds = s => { - if (s < 0) s = 0; - - return [s / 3600, s / 60 % 60, s % 60] - .map(v => `${Math.floor(v).toString().padStart(2, '0')}`) - .join(':'); + 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(':') + ); }; ``` ```js -formatSeconds(-200); // "00:00:00" -formatSeconds(200); // "00:03:20" -formatSeconds(99999); // "27:46:39" +formatSeconds(200); // '00:03:20' +formatSeconds(-200); // '-00:03:20' +formatSeconds(99999); // '27:46:39' ```