From 9d29f6fb05f16d7e0c57a8767d83db9257d44bd2 Mon Sep 17 00:00:00 2001 From: maliming Date: Wed, 26 Feb 2025 16:03:03 +0800 Subject: [PATCH] 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 *************************************************/