2

I researched about converting date into string in ISO format, and I found two methods to do that giving me the same result '2022-07-29T06:46:54.085Z':

  1. (new Date()).toISOString()
  2. JSON.parse(JSON.stringify(new Date()))

Question:

  • Does JS make two approaches/algorithms of converting date or just one function code just call on different object JSON or Date, If So Which one is the best to use?
XMehdi01
  • 5,538
  • 2
  • 10
  • 34
  • json stringfy gives extra quotes – cmgchess Jul 29 '22 at 18:53
  • edited with `JSON.parse` removes those extra quotes! – XMehdi01 Jul 29 '22 at 18:54
  • 3
    I bet JSON.stringify calls toISOString somewhere behind the curtain. Use the one that explicitly does what you want: toISOString. – Corey Ogburn Jul 29 '22 at 18:56
  • 1
    guess this is it https://stackoverflow.com/questions/11491938/issues-with-date-when-using-json-stringify-and-json-parse#comment75700467_19210578. some here https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify#description as well – cmgchess Jul 29 '22 at 18:57

3 Answers3

2

First of all: less code, easier to maintain

So, new Date().toISOString() is simplest way to return string in ISO format.

Regarding question:

No. The output is the same, because of JSON.stringify logic underneath that returns:

JSON.stringify(new Date())
'"2022-07-29T18:58:14.411Z"'

Because:

The instances of Date implement the toJSON() function by returning a string (the same as date.toISOString()). Thus, they are treated as strings.

Source: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify

(new Date).toJSON()
'2022-07-29T18:58:14.411Z'
Alex Strizhak
  • 910
  • 1
  • 12
  • 22
  • If "simple" means less code, then `new Date().toJSON()` is less to type and produces an identical result. ;-) – RobG Jul 31 '22 at 02:39
  • @RobG why so? `'new Date().toJSON()'.length` => 19 `'(new Date).toJSON()'.length` => 19 - no difference ;) – Alex Strizhak Jul 31 '22 at 12:12
  • You suggested using `new Date().toISOString()`, which is longer. `(new Date).toJSON()` relies on a quirk of ECMAScript constructors that is rarely used in practice, so not a good idea—IMHO of course. :-) – RobG Aug 01 '22 at 03:59
0

JSON.parse(JSON.stringify(new Date())) is just the same as new Date().toJSON(). And in the docs for that method we can see

Calling toJSON() returns a string (using toISOString()) representing the Date object's value.

So they're having exactly the same result, calling toISOString() directly is just much more straightforward.

Bergi
  • 630,263
  • 148
  • 957
  • 1,375
0

If an object passed to JSON.stringify has a toJSON method, then it's used to set the value within the JSON text (see SerializeJSONProperty step 2.b).

Date instances have a toJSON method that is defined as returning the same value as Date.prototype.toISOString.

I.e.

date.toJSON() === date.toISOString()

by design.

RobG
  • 142,382
  • 31
  • 172
  • 209