import moment, {Moment} from 'moment'; export function parseNoSeparatedDate (dt: string, format: string, isUTC: boolean): Moment | undefined { let date: Moment | undefined; const getMoment = (dt: Date | string, format?: string) => isUTC ? moment.utc(dt, format) : moment(dt, format); if (dt.length === 8) { // For parsing dates, represented as "DDMMYYYY" (without separators) const d = new Date( parseInt(dt.substring(4, 8)), parseInt(dt.substring(2, 4)) - 1, parseInt(dt.substring(0, 2)) ); if (d instanceof Date && !isNaN(d.getFullYear())) { date = getMoment(d); } } if (!date && dt.length === format.length) { date = getMoment(dt, format); } return date; }