From 7750f530d88ced3f97d49ff95bc91a25ff0d6c3a Mon Sep 17 00:00:00 2001 From: Patrizio Bekerle <patrizio.bekerle@tugraz.at> Date: Mon, 26 Aug 2019 13:14:56 +0200 Subject: [PATCH] Add functions dateToStrippedIsoDT and pad10 --- packages/common/utils.js | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/packages/common/utils.js b/packages/common/utils.js index cc3b60dc..c26afe76 100644 --- a/packages/common/utils.js +++ b/packages/common/utils.js @@ -126,3 +126,25 @@ export const makeId = (length) => { return result; } + +/** + * Pads a number with a 0 so it has two digits + * + * @param n + * @returns {string} + */ +export const pad10 = (n) => { return n < 10 ? '0' + n : n }; + +/** + * Converts a date string to a local iso datetime with stripped seconds and timezone for the datetime-local input + * + * @param date + * @returns {string} + */ +export const dateToStrippedIsoDT = (date) => { + if (!(date instanceof Date)) { + date = new Date(date); + } + + return `${date.getFullYear()}-${pad10(date.getMonth()+1)}-${pad10(date.getDate())}T${pad10(date.getHours())}:${pad10(date.getMinutes())}`; +}; -- GitLab