0

I've seen many articles about this but not quite found what I am looking for yet.

I'm trying to simply check if a string is a date. That string might contains a number such as 2012 though.

var timestamp = Date.parse('foo 2012');

if (isNaN(timestamp) == false) {
  var d = new Date(timestamp);
  console.log('DATE', d)
}else{
   console.log('TEXT');
}

//OUTPUT: DATE Sun Jan 01 2012 00:00:00 GMT+1100 (Australian Eastern Daylight Time)

  • Why does javascript convert foo 2012 into a date when it's clearly not?
  • How can I validate that my string is an actual date ?
brovasi
  • 167
  • 9
  • 2
    Define "is a date" – Jacob Sep 15 '20 at 22:41
  • Every JavaScript environment has its own rules for parsing date strings that aren't in ISO standard format. You can't rely on it being consistent. That's why libraries like Moment exist: you decide what you consider a date string to be (or a list of possibilities), and then you explicitly tell it what to parse. – Pointy Sep 15 '20 at 22:49
  • Please only ask one question per post. For the first question, parsing if unsupported formats is implementation dependent. `Date.parse('foo 2012')` produces different results in different implementations. For the second question, see the second duplicate. – RobG Sep 16 '20 at 03:28

1 Answers1

3

A string is never a date.

A string can represent a date. There are many ways to represent the same date through different string formats.

This means "validating that a string is a date" is an impossible task. What you can do instead is:

  1. Validate that the format of the string conforms to some well-known format and that its contents are valid.
  2. Try to parse a given string as an expected format and either throw an error or return a "guard" value to indicate that the parsing was not a success.

JavaScript took a third option, which is like option 2, only it returns a value indicating its "best efforts" at working with the garbage input. That's apparently why it saw a year in your string and decided "well, I guess I can make a date with this?" As @Pointy pointed out, it's actually in the spec that if the JavaScript string doesn't match one of the two supported formats, the implementer can decide what the result should be.

I'd highly recommend that if you're dealing with date strings, you should stick with a consistent format. I'm a big fan of ISO 8601 date/time strings because they are time zone aware, easy to sort, and are widely supported (including being easily parsed by JavaScript). If you're requiring the strings are in a standard format, it's easier to do things like finding A RegExp that can validate the input.

Jacob
  • 77,566
  • 24
  • 149
  • 228
  • 1
    Give me any string and any date and I can define a mapping of the one to the other. – Jeff Dege Sep 15 '20 at 22:57
  • @JeffDege what if the string is written in Chinese? Also the OP is about validating that a string "is a date". Is "15/08/2019" a date? It is in Europe, but it is not in the US. – Pointy Sep 15 '20 at 23:00
  • Doesn't matter. If I can define the mapping, I can make it whatever I like. – Jeff Dege Sep 15 '20 at 23:20
  • I am just surprised that javascript still convert foo into a date when it's clearly not. – brovasi Sep 15 '20 at 23:40