Advertisement
lukibeni

parse

Dec 13th, 2018
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. parse(value: any): Date | null {
  2.         const parsedDate = super.parse(value);
  3.         if (parsedDate instanceof Date && !isNaN(parsedDate.getTime())) {
  4.             return parsedDate;
  5.         } else {
  6.             let otherDate: Date = null;
  7.             const yearResults = value.match(/[1-9]{1}[0-9]{3}/g);
  8.             if (yearResults && yearResults.length > 0) {
  9.                 const year = yearResults[0];
  10.                 otherDate = new Date(year);
  11.                 const monthResults = (value.replace(year, '')).match(/[0-9]{1}[1-9]{1}/g);
  12.                 if (monthResults && monthResults.length > 0) {
  13.                     const month = monthResults[0];
  14.                     otherDate = new Date(year, month - 1);
  15.                     const dayResults = (value.replace(year, '').replace(month, '')).match(/[0-9]{1}[1-9]{1}/g);
  16.                     if (dayResults && dayResults.length > 0) {
  17.                         const day = dayResults[0];
  18.                         otherDate = new Date(year, month - 1, day);
  19.                     }
  20.                 }
  21.             }
  22.             if (otherDate != null && !isNaN(otherDate.getTime())) {
  23.                 return otherDate;
  24.             } else {
  25.                 return null;
  26.             }
  27.         }
  28.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement