UNPKG

@true-directive/base

Version:

The set of base classes for the TrueDirective Grid

180 lines (179 loc) 5.71 kB
import { Dates } from '../common/dates.class'; // Parse and format DateTime var DateParserFormatter = /** @class */ (function () { function DateParserFormatter() { } // Creating Invalid Date DateParserFormatter.invalidDate = function () { return new Date('*'); }; DateParserFormatter.daysInMonth = function (y, m) { return new Date(y, m, 0).getDate(); }; DateParserFormatter.parse = function (value, mask) { if (value === '') { return null; } var res = value; var sectionPos = 0; var d = 1; var m = 1; var y = 1970; var hh = 0; var mi = 0; var ss = 0; var ms = 0; var tt = ''; for (var i = 0; i < mask.sections.length; i++) { var incomplete = false; var section = mask.sections[i]; var datePart = section.sectionType.datePart; if (datePart === null) { // Not datetime component continue; } var v = section.extract(res, sectionPos); sectionPos = v.nextSectionPos(); // Get section value var s = v.section.value(); var n = void 0; n = NaN; if (section.isNumeric()) { if (s.indexOf(mask.settings.placeholder) >= 0) { // Contains placeholders return DateParserFormatter.invalidDate(); } n = section.numericValue(section.removePlaceholders(s)); if (n < section.sectionType.min || n > section.sectionType.max) { return DateParserFormatter.invalidDate(); } } else { if (section.hasOptions()) { n = section.sectionType.options.indexOf(s); if (n < 0) { return DateParserFormatter.invalidDate(); } n++; } } if (isNaN(n)) { return DateParserFormatter.invalidDate(); } // Time components... if (datePart === 'H') { hh = n; } if (datePart === 'h') { hh = n; if (hh === 12) { hh = 0; } } if (datePart === 'tt') { tt = s; } if (datePart === 'mi') { mi = n; } if (datePart === 'ss') { ss = n; } if (datePart === 'ms') { ms = n; } // Date components... if (datePart === 'd') { d = n; } if (datePart === 'm') { m = n; } if (datePart === 'yy') { y = n < 50 ? 2000 + n : 1900 + n; } if (datePart === 'yyyy') { if (n < 100 && incomplete) { y = n < 50 ? 2000 + n : 1900 + n; } else { y = n; } } } if (tt.toLowerCase() === 'pm') { hh += 12; } // We should check number of days in month var maxDays = DateParserFormatter.daysInMonth(y, m); if (d > maxDays) { return DateParserFormatter.invalidDate(); } return new Date(y, m - 1, d, hh, mi, ss, ms); }; DateParserFormatter.format = function (date, mask) { if (Dates.isEmpty(date)) { return ''; } var res = ''; for (var i = 0; i < mask.sections.length; i++) { var section = mask.sections[i]; var datePart = section.sectionType.datePart; var n = NaN; if (datePart === 'yyyy') { n = date.getFullYear(); } if (datePart === 'yy') { n = date.getFullYear(); if (n >= 2000) { n -= 2000; } else { n -= 1900; } } if (datePart === 'm') { n = date.getMonth() + 1; } if (datePart === 'd') { n = date.getDate(); } if (datePart === 'H') { n = date.getHours(); } if (datePart === 'h') { n = date.getHours(); if (n === 0) { n = 12; } else { if (n > 12) { n -= 12; } } } if (datePart === 'mi') { n = date.getMinutes(); } if (datePart === 'ss') { n = date.getSeconds(); } if (datePart === 'ms') { n = date.getMilliseconds(); } if (datePart === 'tt') { n = date.getHours() >= 12 ? 2 : 1; } var s = ''; if (section.hasOptions()) { s = section.sectionType.options[n - 1]; } else { s = section.autoCorrectVal(n + ''); } res += s + section.delimiter; } return res; }; return DateParserFormatter; }()); export { DateParserFormatter };