From 009ec66c105c2e138024fd87c7526a1e0cf845a8 Mon Sep 17 00:00:00 2001 From: PMKuipers Date: Fri, 28 Jul 2023 23:42:57 +0200 Subject: [PATCH] Added localized date formatting function --- amd/src/string-helper.js | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/amd/src/string-helper.js b/amd/src/string-helper.js index d14535d..6d4f38a 100644 --- a/amd/src/string-helper.js +++ b/amd/src/string-helper.js @@ -54,4 +54,22 @@ export function load_stringkeys(string_keys){ } return string_keys; } +/** + * Format a date according to localized custom + * @param {Date|string} d The date to convert + * @param {boolean} short Short format (default false) + * @returns {string} + */ +export function format_date(d,short){ + if(!(d instanceof Date)){ + d = new Date(d); + } + let monthformat = "short"; + if(short){ + monthformat = "numeric"; + } + return d.toLocaleDateString(document.documentElement.lang,{ + year: 'numeric', month: monthformat, day: 'numeric' + }); +} \ No newline at end of file