@angular-jz/jz
Version:
jim jz
116 lines • 5.23 kB
JavaScript
;
//import { formatDate } from './i18n/format_date';
Object.defineProperty(exports, "__esModule", { value: true });
var DateUtility = /** @class */ (function () {
function DateUtility() {
}
///判断是否为有效的日期值
DateUtility.isDate = function (value) {
if (!/^(\d{4})\-(\d{1,2})\-(\d{1,2})$/.test(value))
return false;
return (!isNaN(new Date(value).getFullYear()));
};
//public static isDate(value: any): value is Date {
// return value instanceof Date && !isNaN(value.valueOf());
//}
DateUtility.parseFormat = function (format) {
var validParts = /dd?|DD?|mm?|MM?|yy(?:yy)?/g;
var separators = format.replace(validParts, '\0').split('\0'), parts = format.match(validParts);
if (!separators || !separators.length || !parts || parts.length === 0) {
throw new Error("Invalid date format.");
}
return { separators: separators, parts: parts };
};
;
///格式化日期字符串
DateUtility.formatDate = function (date, format, language) {
if (!date)
return '';
date = new Date(date);
//if (typeof format === 'string')
if (!format)
format = 'yyyy-mm-dd';
var formatObject = DateUtility.parseFormat(format);
var val = {
d: date.getDate(),
//D: dates[language].daysShort[date.getUTCDay()],
//DD: dates[language].days[date.getUTCDay()],
m: date.getMonth() + 1,
//M: dates[language].monthsShort[date.getUTCMonth()],
//MM: dates[language].months[date.getUTCMonth()],
yy: date.getFullYear().toString().substring(2),
yyyy: date.getFullYear(),
dd: null,
mm: null
};
val.dd = (val.d < 10 ? '0' : '') + val.d;
val.mm = (val.m < 10 ? '0' : '') + val.m;
var dateArray = [];
var seps = formatObject.separators.slice(); // $.extend([], formatObject.separators);
for (var i = 0, cnt = formatObject.parts.length; i <= cnt; i++) {
if (seps.length)
dateArray.push(seps.shift());
dateArray.push(val[formatObject.parts[i]]);
}
return dateArray.join('');
};
;
//public static formatDate(value: any, format = 'mediumDate', timezone?: string, locale?: string): string | null {
// if (value == null || value === '' || value !== value) return null;
// if (typeof value === 'string') {
// value = value.trim();
// }
// let date: Date;
// if (this.isDate(value)) {
// date = value;
// } else if (!isNaN(value - parseFloat(value))) {
// date = new Date(parseFloat(value));
// } else if (typeof value === 'string' && /^(\d{4}-\d{1,2}-\d{1,2})$/.test(value)) {
// /**
// * For ISO Strings without time the day, month and year must be extracted from the ISO String
// * before Date creation to avoid time offset and errors in the new Date.
// * If we only replace '-' with ',' in the ISO String ("2015,01,01"), and try to create a new
// * date, some browsers (e.g. IE 9) will throw an invalid Date error
// * If we leave the '-' ("2015-01-01") and try to create a new Date("2015-01-01") the timeoffset
// * is applied
// * Note: ISO months are 0 for January, 1 for February, ...
// */
// const [y, m, d] = value.split('-').map((val: string) => +val);
// date = new Date(y, m - 1, d);
// } else {
// date = new Date(value);
// }
// if (!this.isDate(date)) {
// let match: RegExpMatchArray | null;
// if ((typeof value === 'string') && (match = value.match(this.ISO8601_DATE_REGEX))) {
// date = this.isoStringToDate(match);
// } else {
// throw "formatDate error";
// }
// }
// return formatDate(date, format, locale || 'en-US', timezone);
//}
/** @internal */
DateUtility.isoStringToDate = function (match) {
var date = new Date(0);
var tzHour = 0;
var tzMin = 0;
var dateSetter = match[8] ? date.setUTCFullYear : date.setFullYear;
var timeSetter = match[8] ? date.setUTCHours : date.setHours;
if (match[9]) {
tzHour = +(match[9] + match[10]);
tzMin = +(match[9] + match[11]);
}
dateSetter.call(date, +(match[1]), +(match[2]) - 1, +(match[3]));
var h = +(match[4] || '0') - tzHour;
var m = +(match[5] || '0') - tzMin;
var s = +(match[6] || '0');
var ms = Math.round(parseFloat('0.' + (match[7] || 0)) * 1000);
timeSetter.call(date, h, m, s, ms);
return date;
};
DateUtility.ISO8601_DATE_REGEX = /^(\d{4})-?(\d\d)-?(\d\d)(?:T(\d\d)(?::?(\d\d)(?::?(\d\d)(?:\.(\d+))?)?)?(Z|([+-])(\d\d):?(\d\d))?)?$/;
return DateUtility;
}());
exports.DateUtility = DateUtility;
//# sourceMappingURL=date-utility.js.map