Browse Source

Merge pull request #22226 from abpframework/abpjs-patch

Refactor date handling in abp.js and add normalizeString function for date formatting
pull/22231/head
maliming 12 months ago
committed by GitHub
parent
commit
041098cf50
No known key found for this signature in database GPG Key ID: B5690EEEBB952194
  1. 77
      npm/packs/core/src/abp.js

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

@ -752,53 +752,56 @@ 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 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());
// 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;
}
return new Date();
};
abp.clock.normalize = function (date) {
var kind = abp.clock.kind;
if (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();
}
function padZero(num) {
return num < 10 ? '0' + num : num;
}
if (kind === 'Utc') {
return toUtc(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());
};
// Normalize date string to locale date string that will be displayed to user
abp.clock.normalizeToLocaleString = function (dateString) {
if (!dateString) {
return dateString;
}
var date = new Date(dateString);
if (isNaN(date)) {
return dateString;
}
//TODO: Get timezone setting and pass it to toLocaleString
return date.toLocaleString();
}
/* FEATURES *************************************************/
abp.features = abp.features || {};

Loading…
Cancel
Save