From 3f11b54f41a8824dcfd3fd5a1de371fd4b2b19e9 Mon Sep 17 00:00:00 2001 From: liangshiwei Date: Wed, 26 Feb 2025 14:07:27 +0800 Subject: [PATCH 1/5] Refactor date handling in abp.js and add normalizeString function for date formatting --- npm/packs/core/src/abp.js | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/npm/packs/core/src/abp.js b/npm/packs/core/src/abp.js index 45b8ac3ba3..35b186d0f0 100644 --- a/npm/packs/core/src/abp.js +++ b/npm/packs/core/src/abp.js @@ -765,7 +765,7 @@ var abp = abp || {}; }; var toUtc = function (date) { - return Date.UTC( + return new Date(Date.UTC( date.getUTCFullYear(), date.getUTCMonth(), date.getUTCDate(), @@ -773,7 +773,7 @@ var abp = abp || {}; date.getUTCMinutes(), date.getUTCSeconds(), date.getUTCMilliseconds() - ); + )); }; abp.clock.now = function () { @@ -799,6 +799,25 @@ var abp = abp || {}; } }; + abp.clock.normalizeString = function (dateString) { + var date = abp.clock.normalize(new Date(dateString)); + + var kind = abp.clock.kind; + + if(kind === 'Local' || kind === 'Unspecified'){ + return date.getFullYear() + "-" + + String(date.getMonth() + 1).padStart(2, "0") + "-" + + String(date.getDate()).padStart(2, "0") + "T" + + String(date.getHours()).padStart(2, "0") + ":" + + String(date.getMinutes()).padStart(2, "0") + ":" + + String(date.getSeconds()).padStart(2, "0") + ".000"; + } + + if(kind === 'Utc'){ + return date.toISOString(); + } + } + /* FEATURES *************************************************/ abp.features = abp.features || {}; From ec2e78aa05bd2bcc4cef0da0613b2aeefb38150a Mon Sep 17 00:00:00 2001 From: maliming Date: Wed, 26 Feb 2025 14:44:09 +0800 Subject: [PATCH 2/5] Update abp.js --- npm/packs/core/src/abp.js | 41 ++++++++++++++++++++++++++++----------- 1 file changed, 30 insertions(+), 11 deletions(-) diff --git a/npm/packs/core/src/abp.js b/npm/packs/core/src/abp.js index 35b186d0f0..2eeb3234aa 100644 --- a/npm/packs/core/src/abp.js +++ b/npm/packs/core/src/abp.js @@ -797,25 +797,44 @@ var abp = abp || {}; if (kind === 'Utc') { return toUtc(date); } + + return date; }; abp.clock.normalizeString = function (dateString) { - var date = abp.clock.normalize(new Date(dateString)); - - var kind = abp.clock.kind; + if (!dateString) { + return dateString; + } - if(kind === 'Local' || kind === 'Unspecified'){ - return date.getFullYear() + "-" + - String(date.getMonth() + 1).padStart(2, "0") + "-" + - String(date.getDate()).padStart(2, "0") + "T" + - String(date.getHours()).padStart(2, "0") + ":" + - String(date.getMinutes()).padStart(2, "0") + ":" + - String(date.getSeconds()).padStart(2, "0") + ".000"; + var date = new Date(dateString); + if (isNaN(date)) { + return dateString; } - if(kind === 'Utc'){ + date = abp.clock.normalize(date); + + if(abp.clock.kind === 'Utc'){ return date.toISOString(); } + + function padZero(num) { + return num < 10 ? '0' + num : num; + } + + function padMilliseconds(num) { + if (num < 10) return '00' + num; + if (num < 100) return '0' + num; + return num; + } + + // yyyy-MM-ddTHH:mm:ss.SSS + return date.getFullYear() + '-' + + padZero(date.getMonth() + 1) + '-' + + padZero(date.getDate()) + 'T' + + padZero(date.getHours()) + ':' + + padZero(date.getMinutes()) + ':' + + padZero(date.getSeconds()) + '.' + + padMilliseconds(date.getMilliseconds()); } /* FEATURES *************************************************/ From 7c84e6ad13c0b50dd5aee38957bdd16f1b4eae44 Mon Sep 17 00:00:00 2001 From: maliming Date: Wed, 26 Feb 2025 14:52:05 +0800 Subject: [PATCH 3/5] Improve date normalization in abp.js to handle invalid dates --- npm/packs/core/src/abp.js | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/npm/packs/core/src/abp.js b/npm/packs/core/src/abp.js index 2eeb3234aa..a86ce7c25c 100644 --- a/npm/packs/core/src/abp.js +++ b/npm/packs/core/src/abp.js @@ -784,9 +784,13 @@ var abp = abp || {}; }; abp.clock.normalize = function (date) { + if (!date || Object.prototype.toString.call(date) !== '[object Date]' || isNaN(date)) { + return date; + } + var kind = abp.clock.kind; - if (kind === 'Unspecified') { + if (!kind || kind === 'Unspecified') { return date; } From 9d29f6fb05f16d7e0c57a8767d83db9257d44bd2 Mon Sep 17 00:00:00 2001 From: maliming Date: Wed, 26 Feb 2025 16:03:03 +0800 Subject: [PATCH 4/5] Refactor date normalization in abp.js. --- npm/packs/core/src/abp.js | 91 +++++++++++---------------------------- 1 file changed, 26 insertions(+), 65 deletions(-) diff --git a/npm/packs/core/src/abp.js b/npm/packs/core/src/abp.js index a86ce7c25c..4b245af679 100644 --- a/npm/packs/core/src/abp.js +++ b/npm/packs/core/src/abp.js @@ -752,60 +752,43 @@ var abp = abp || {}; return abp.clock.kind === 'Utc'; }; - var toLocal = function (date) { - return new Date( - date.getFullYear(), - date.getMonth(), - date.getDate(), - date.getHours(), - date.getMinutes(), - date.getSeconds(), - date.getMilliseconds() - ); - }; - - var toUtc = function (date) { - return new Date(Date.UTC( - date.getUTCFullYear(), - date.getUTCMonth(), - date.getUTCDate(), - date.getUTCHours(), - date.getUTCMinutes(), - date.getUTCSeconds(), - date.getUTCMilliseconds() - )); - }; - - abp.clock.now = function () { - if (abp.clock.kind === 'Utc') { - return toUtc(new Date()); - } - return new Date(); - }; - + // Normalize Date object or date string that will be sent to server abp.clock.normalize = function (date) { - if (!date || Object.prototype.toString.call(date) !== '[object Date]' || isNaN(date)) { + if (!date) { return date; } - var kind = abp.clock.kind; - - if (!kind || kind === 'Unspecified') { + var dateObj = date instanceof Date ? date : new Date(date); + if (isNaN(dateObj)) { return date; } - if (kind === 'Local') { - return toLocal(date); + if (abp.clock.kind === 'Utc') { + return dateObj.toISOString(); } - if (kind === 'Utc') { - return toUtc(date); + function padZero(num) { + return num < 10 ? '0' + num : num; } - return date; + function padMilliseconds(num) { + if (num < 10) return '00' + num; + if (num < 100) return '0' + num; + return num; + } + + // yyyy-MM-ddTHH:mm:ss.SSS + return dateObj.getFullYear() + '-' + + padZero(dateObj.getMonth() + 1) + '-' + + padZero(dateObj.getDate()) + 'T' + + padZero(dateObj.getHours()) + ':' + + padZero(dateObj.getMinutes()) + ':' + + padZero(dateObj.getSeconds()) + '.' + + padMilliseconds(dateObj.getMilliseconds()); }; - abp.clock.normalizeString = function (dateString) { + // Normalize date string getting from server to Local + abp.clock.normalizeToLocaleDate = function (dateString) { if (!dateString) { return dateString; } @@ -814,31 +797,9 @@ var abp = abp || {}; if (isNaN(date)) { return dateString; } - - date = abp.clock.normalize(date); - - if(abp.clock.kind === 'Utc'){ - return date.toISOString(); - } - - function padZero(num) { - return num < 10 ? '0' + num : num; - } - - function padMilliseconds(num) { - if (num < 10) return '00' + num; - if (num < 100) return '0' + num; - return num; - } - // yyyy-MM-ddTHH:mm:ss.SSS - return date.getFullYear() + '-' + - padZero(date.getMonth() + 1) + '-' + - padZero(date.getDate()) + 'T' + - padZero(date.getHours()) + ':' + - padZero(date.getMinutes()) + ':' + - padZero(date.getSeconds()) + '.' + - padMilliseconds(date.getMilliseconds()); + //TODO: Get timezone setting and pass it to toLocaleString + return date.toLocaleString(); } /* FEATURES *************************************************/ From cfde7abbdf8a2eb57071428d5caf58efd32bbc95 Mon Sep 17 00:00:00 2001 From: maliming Date: Wed, 26 Feb 2025 16:10:03 +0800 Subject: [PATCH 5/5] Rename normalization functions in abp.js for clarity and consistency --- npm/packs/core/src/abp.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/npm/packs/core/src/abp.js b/npm/packs/core/src/abp.js index 4b245af679..05c9f3bbe5 100644 --- a/npm/packs/core/src/abp.js +++ b/npm/packs/core/src/abp.js @@ -752,8 +752,8 @@ var abp = abp || {}; return abp.clock.kind === 'Utc'; }; - // Normalize Date object or date string that will be sent to server - abp.clock.normalize = function (date) { + // Normalize Date object or date string to standard string format that will be sent to server + abp.clock.normalizeToString = function (date) { if (!date) { return date; } @@ -787,8 +787,8 @@ var abp = abp || {}; padMilliseconds(dateObj.getMilliseconds()); }; - // Normalize date string getting from server to Local - abp.clock.normalizeToLocaleDate = function (dateString) { + // Normalize date string to locale date string that will be displayed to user + abp.clock.normalizeToLocaleString = function (dateString) { if (!dateString) { return dateString; }