>>> const z = { d: new Date() }
undefined
>>> typeof z.d
"object"
>>> const a = JSON.parse(JSON.stringify(z))
undefined
>>> typeof a.d
"string"
>>>
After the roundtrip, the property `d` is now a string, not a Date object. >>> const z = { d: new Date() }
undefined
>>> typeof z.d
"object"
>>> const a = JSON.parse(JSON.stringify(z))
undefined
>>> typeof a.d
"string"
>>>
After the roundtrip, the property `d` is now a string, not a Date object.It's too bad this library (and approach) never took off, but it's there to use nevertheless.
Even without iso-fns, it's not as if mid level developer can't figure out how to write a function to perform a specific operation on an ISO date string.
After over 10 years of web development, I do think this is the best known approach and is even better than storing UNIX time. Even if there were no existing libraries to help with manipulating ISO strings, I will gladly take the inconvenience in exchange for the rest of the advantages.
With ISO strings, you don't get any behavioral quirks from Date; they are totally compatible with JSON; you can store the time zone along with the time; there is nothing to stop you from changing the time zone while leaving all the other values intact; ISO strings also support durations.
Basically, they support most of what a software developer will be expected to do with times and dates but without any behavior or assumptions about the client time zone that introduce bugs like with Date. `<input type="datetime-local">` also uses ISO strings out of the box, which can be really convenient, and values from `<input type="date">` can be easily appended into an actual ISO string.
The following isn't applicable for pure JSON responses, but is applicable for primitive types:
An additional condition for bigint is necessary, which is a primitive type. Otherwise JSON.stringify throws:
>>> const x = { i: BigInt("0x1fffffffffffff") }
undefined
>>> typeof x.i
"bigint"
>>> const b = JSON.parse(JSON.stringify(x))
Exception: TypeError: JSON.stringify cannot serialize BigInt.
>>>
... and for the symbol primitive, which JSON.stringifys to undefined, and thus can't be JSON.parsed.