From 02ecdcb10f459007e23d43ed7ed49b84e9c370ca Mon Sep 17 00:00:00 2001 From: Michael Goldspinner Date: Sat, 13 Jan 2018 10:16:09 -0500 Subject: [PATCH] Improved getColonTime() Reduced function code to a single line. Uses Date.toTimeString() and String.slice() to select just the colon time from stirng. --- snippets/getColonTimeFromDate.md | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/snippets/getColonTimeFromDate.md b/snippets/getColonTimeFromDate.md index e1e7b1990..7d7e7f297 100644 --- a/snippets/getColonTimeFromDate.md +++ b/snippets/getColonTimeFromDate.md @@ -3,12 +3,7 @@ Formats hour, minute, and second time integers from Date into stringified colon representation. Formats hour integer to 12 hour clock and maintains digit lengths to match (HH:MM:SS). ```js -const getColonTimeFromDate = date => { - let times = [ date.getHours(), date.getMinutes(), date.getSeconds() ]; - times[0] = times[0] === 0 || times[0] === 12 || times[0] === 24 ? 12 : times[0] % 12; - - return times.map( time => time.toString().padStart(2, "0") ).join(":"); -} +const getColonTimeFromDate = date => date.toTimeString().slice(0, 8) ``` ```js