How to auto recognize date format

Viewed 58

When I'm parsing xls data, I'm facing with a lot of different dates like date, datetime, or only time.

Example time: enter image description here

Example date without time: enter image description here

In document the first value is just a time, second just a date.

How can I figure out this case and recognize where is the date, where the time and where the DateTime?

1 Answers

so it's either date, time or datetime. One easy way would be to write 3 regular expressions to determine which one you're dealing with. Something like

function getType(dateStr) {
  if (/^\w{3} \w{3} \d{1,2} \d{4} \d{2}:\d{2}:\d{2} GMT\+\d+$/.test(dateStr)) { // matches 'Sat Mar 30 1899 05:24:10 GMT+030'
    return 'datetime';
  }
  if (otherRegExMatchingOnlyTime.test(dateStr)) {
    return 'time';
  }
  if (otherRegExMatchingOnlyDate.test(dateStr) {
    return 'date';
  }

  throw new Error(`Unsupported date format of ${dateStr}`);
}

I'm not writing all regular expressions here, since I don't have any proper examples of your date-only or time-only values

Related