Browse Source

Refactor date normalization in abp.js.

pull/22226/head
maliming 2 years ago
parent
commit
9d29f6fb05
No known key found for this signature in database GPG Key ID: A646B9CB645ECEA4
  1. 91
      npm/packs/core/src/abp.js

91
npm/packs/core/src/abp.js

@ -752,60 +752,43 @@ var abp = abp || {};
return abp.clock.kind === 'Utc'; return abp.clock.kind === 'Utc';
}; };
var toLocal = function (date) { // Normalize Date object or date string that will be sent to server
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();
};
abp.clock.normalize = function (date) { abp.clock.normalize = function (date) {
if (!date || Object.prototype.toString.call(date) !== '[object Date]' || isNaN(date)) { if (!date) {
return date; return date;
} }
var kind = abp.clock.kind; var dateObj = date instanceof Date ? date : new Date(date);
if (isNaN(dateObj)) {
if (!kind || kind === 'Unspecified') {
return date; return date;
} }
if (kind === 'Local') { if (abp.clock.kind === 'Utc') {
return toLocal(date); return dateObj.toISOString();
} }
if (kind === 'Utc') { function padZero(num) {
return toUtc(date); 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) { if (!dateString) {
return dateString; return dateString;
} }
@ -814,31 +797,9 @@ var abp = abp || {};
if (isNaN(date)) { if (isNaN(date)) {
return dateString; 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 //TODO: Get timezone setting and pass it to toLocaleString
return date.getFullYear() + '-' + return date.toLocaleString();
padZero(date.getMonth() + 1) + '-' +
padZero(date.getDate()) + 'T' +
padZero(date.getHours()) + ':' +
padZero(date.getMinutes()) + ':' +
padZero(date.getSeconds()) + '.' +
padMilliseconds(date.getMilliseconds());
} }
/* FEATURES *************************************************/ /* FEATURES *************************************************/

Loading…
Cancel
Save