From d91d51a60a89d701d238101aa4e30c4486be3c78 Mon Sep 17 00:00:00 2001 From: James Collins Date: Tue, 28 Feb 2023 09:44:31 +1000 Subject: [PATCH] added support for ISO 8601 formats --- resources/js/helpers/datetime.ts | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/resources/js/helpers/datetime.ts b/resources/js/helpers/datetime.ts index 55e7384..6f6fab3 100644 --- a/resources/js/helpers/datetime.ts +++ b/resources/js/helpers/datetime.ts @@ -84,10 +84,23 @@ export class SMDate { const order = (options.format || "dmy").toLowerCase().split(""); options.utc = options.utc || false; - // Split the date string into an array of components based on the length of each date component - const components = dateString.split(/[ /-]/); + let components = []; let time = ""; + // Test if the dateString is in ISO 8601 + if ( + /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(\.\d{1,10})?Z$/i.test( + dateString + ) + ) { + options.format = "YMd"; + [dateString, time] = dateString.split("T"); + time = time.slice(0, -8); + } + + // Split the date string into an array of components based on the length of each date component + components = dateString.split(/[ /-]/); + for (let i = 0; i < components.length; i++) { if (components[i].includes(":")) { time = components[i]; @@ -210,7 +223,7 @@ export class SMDate { checkDay = new Date(isoDate).getUTCDate(); checkHours = parseInt(isoDate.substring(11, 13), 10); checkMinutes = parseInt(isoDate.substring(14, 16), 10); - checkSeconds = parseInt(isoDate.substring(17, 18), 10); + checkSeconds = parseInt(isoDate.substring(17, 19), 10); } else { checkYear = date.getFullYear(); checkMonth = date.getMonth() + 1;