6 ms·
There's plenty of good reasons one doesn't exist. JSON is not JS-specific. It is a standard interchange format used by thousands of languages, many of which hav
by BillinghamJ 7y ago
There's plenty of good reasons one doesn't exist. JSON is not JS-specific. It is a standard interchange format used by thousands of languages, many of which have very different date implementations.
If you did have a JSON date, how would you decide what it was? Would it be a timestamp, or a civil date-time? Would it have timezones? Offsets? Locations? Would there be a database along with it required to understand it correctly?
Just stick with ISO8601 strings.
- ryanbrunner 7y agoA 'date' type could be as simple as syntax indicating that a particular ISO8601 string IS a date. Right now, there's no reasonable way to infer that unless you're already aware of the schema of the JSON object you're receiving. You could make the same argument with numbers - there's no reason you couldn't just pass strings containing the number - but there's advantages to being able to distinguish between a string and a number when the schema is unknown.
- BillinghamJ 7y agoIf you're not aware of the schema, how would you use the value? Same as any other use of strings - enums, types, etc
- goto11 7y agoThat is not a good reason though. The spec could just decide on the wire format like with strings and numbers, and clients would have to translate into the date type of the language or platform. The reason JSON doesn't have dates is simply because JavaScript doesn't have date literals. You can write new Date(...) in JavaScript, but allowing that in JSON would have opened a whole can of worms.
- BillinghamJ 7y agoJSON started already having incompatibilities with JS literals - JS objects are only just being made a superset of JSON in ES2019
- paulddraper 7y agoWhy have a JSON number? Why not "just stick with" decimal strings?
- dwaltrip 7y agoI see your point, but numbers are easier to standardize than dates, so there is some logic to drawing the line there. It has to be drawn somewhere.
- paulddraper 7y ago> numbers are easier to standardize than dates Is this a joke? JSON numbers are....I don't know what they even are. JavaScript -- the originator of JSON -- can't support 64-bit integers in JSON. Nor can it support decimal fractions in JSON, despite JSON using decimals. And yet JSON numbers also aren'ty IEEE-754 floating point numbers, as they lack +/-Infinity and NaN. JSON numbers are ill-defined and unique from every other "number" concept in any language or format.