From 31505dde7ec5e8077a5a72e7e50d4e5d7203432d Mon Sep 17 00:00:00 2001 From: Linus Groh Date: Fri, 10 Apr 2020 15:22:58 +0100 Subject: LibJS: Add String.prototype.pad{Start,End}() --- Base/home/anon/js/date.js | 29 +++++++---------------------- 1 file changed, 7 insertions(+), 22 deletions(-) (limited to 'Base/home/anon') diff --git a/Base/home/anon/js/date.js b/Base/home/anon/js/date.js index 56a40d619c..160786e0bc 100644 --- a/Base/home/anon/js/date.js +++ b/Base/home/anon/js/date.js @@ -1,29 +1,14 @@ var now = Date.now(); console.log("Unix timestamp: " + now / 1000); -// FIXME: We need String.prototype.padStart() :^) var d = new Date(); var year = d.getFullYear(); -var month = d.getMonth() + 1; -if (month < 10) - month = "0" + month; -var day = d.getDate(); -if (day < 10) - day = "0" + day; -var hours = d.getHours(); -if (hours < 10) - hours = "0" + hours; -var minutes = d.getMinutes(); -if (minutes < 10) - minutes = "0" + minutes; -var seconds = d.getSeconds(); -if (seconds < 10) - seconds = "0" + seconds; -var milliseconds = d.getMilliseconds(); -if (milliseconds < 10) { - milliseconds = "00" + milliseconds; -} else if (milliseconds < 100) { - milliseconds = "0" + milliseconds; -} +var month = (d.getMonth() + 1).toString().padStart(2, "0"); +var day = d.getDate().toString().padStart(2, "0"); +var hours = d.getHours().toString().padStart(2, "0"); +var minutes = d.getMinutes().toString().padStart(2, "0"); +var seconds = d.getSeconds().toString().padStart(2, "0"); +var milliseconds = d.getMilliseconds().toString().padStart(3, "0"); + console.log("Date: " + year + "-" + month + "-" + day); console.log("Time: " + hours + ":" + minutes + ":" + seconds + "." + milliseconds); -- cgit v1.2.3