0

Title,

> new Date('clustering, metric 1,')
> Mon Jan 01 2001 00:00:00 GMT+0100 (CET)

So Date.parse('clustering, metric 1,') return true, it's an issue for my type detection, how can this be parsed as a date, it's not even close.

edit:
@evolutionxbox that's probably the reason, it's very naive. I guess the solution is to make a regex for every date format. If someone got references about date detection he's welcome.

François MENTEC
  • 1,150
  • 4
  • 12
  • 25
  • 4
    Your type detection consists of trying to parse random values as dates…? I'd say the problem is *there*. – deceze Jul 20 '17 at 13:19
  • 1
    The same way: `> new Date('ffffff, ffff, 1'); < Mon Jan 01 2001 00:00:00 GMT+0000 (GMT Standard Time)` followed by `> new Date('1'); < Mon Jan 01 2001 00:00:00 GMT+0000 (GMT Standard Time)` – eithed Jul 20 '17 at 13:19
  • 1
    It's reading everything before the `1` as the month. – evolutionxbox Jul 20 '17 at 13:19
  • use this instead? https://jsfiddle.net/dalinhuang/x32xj5de/ – Dalin Huang Jul 20 '17 at 13:21
  • 1
    You can only reliably parse a string to a Date if you know the format and provide that format to the parser. Anything else is leaving it to chance. The built-in parser doesn't accept a parse format, so it is extremely unreliable (as you've discovered). :-( BTW, in Safari `new Date('clustering, metric 1,')` returns an invalid date. – RobG Jul 20 '17 at 20:45

1 Answers1

0

Use moment.js to parse dates. You can check if a date is valid this way:

moment("clustering, metric 1,", ["MM-DD-YYYY", "YYYY-MM-DD"], true).isValid(); // false

The 3rd parameter instructs moment to use strict parsing (available in version 2.3.0 and above).

Lukasz Wiktor
  • 19,644
  • 5
  • 69
  • 82
  • While suggesting to use something other than the built-in parser is a good idea, this doesn't actually answer the question. – RobG Jul 20 '17 at 23:55
  • That's true it doesn't answer the question. However, it helps to solve the problem, I hope. – Lukasz Wiktor Jul 21 '17 at 05:25