provide me a suitable way to check the format.
Here is an example using moment:
export namespace Dates { export const dateFormats = { dateTimeFormat: 'DD/MM/YYYY HH:mm', dateOnlyFormat: 'DD/MM/YYYY', monthYearFormat: 'MM/YYYY', isoFormat: 'YYYY-MM-DD HH:mm:SS ', // Corresponds to '2016-01-04T13:00:00Z' } export function toDate(value: string, /** * The default has *all* the formats * - This is because strict date type validations * - are done by passing in explicit limited sets. **/ formats = [ dateFormats.dateTimeFormat, dateFormats.dateOnlyFormat, dateFormats.isoFormat, dateFormats.monthYearFormat, ] ): { valid: boolean, date: Date | null } { if (!value || !value.trim().length) { return { valid: true, date: null }; } let trimmed = value.trim(); if (!formats.some(format => format.length == trimmed.length)) { return { valid: false, date: null }; } // http://momentjs.com/docs/#/parsing/string-formats/ let date = moment(value, formats, true); if (!date.isValid()) { return { valid: false, date: null }; } return { valid: true, date: date.toDate() }; } }
Feel free to change the date formats as needed 🌹