0

I have a key-value list which is displayed in a tabular format where the key is the label. Some of the values are JSON-date-strings in ISO 8601/XKCD format (e.g. 2012-04-23T18:25:43.511Z). How can I separate them from normal strings in order to display them as a date?

Currently, I am using !isNaN(new Date(string)) which is true when the date-string is valid. This works fine because my values are non-numerical BUT this might change in the future. How can I make sure that only valid JSON-date-strings are formatted as a date?

I am not looking for regex or similar solutions.

leonheess
  • 16,068
  • 14
  • 77
  • 112
  • 2
    *"I am not looking for regex or similar solutions."* Why not? What kind of solution are you looking for? I guess you could write a parser based on https://tools.ietf.org/html/rfc3339#appendix-A – Felix Kling Sep 08 '20 at 08:10
  • @FelixKling One that uses JS built-in date tools if possible. Or do you think there is no such solution? – leonheess Sep 08 '20 at 08:16
  • 1
    Probably not in a 100% predictable way. While the ECMAScript specification specifies which date format is acceptable (which is not actually complete ISO 8601), it also says that *"If the String does not conform to that format the function may fall back to any implementation-specific heuristics or implementation-specific date formats."*. Relevant part of the spec: https://www.ecma-international.org/ecma-262/10.0/#sec-date.parse . – Felix Kling Sep 08 '20 at 08:23

0 Answers0