35

I was checking how to display JavaScript date in the following format: YYYY-MM-DDTHH:mm:ss.sssZ, but I saw two methods doing this: .toJSON() and .toISOString(). Is there some real difference between them?

cbdeveloper
  • 27,898
  • 37
  • 155
  • 336
Almir Sarajčić
  • 1,520
  • 3
  • 16
  • 19
  • 5
    "JSON dates have the same format as the ISO-8601 standard". Use MDN instead of W3Schools anyways – Ian Apr 24 '13 at 17:33

3 Answers3

36

One convenient difference is that if you have an invalid date, .toJSON() will output null. However, .toISOString()'s behavior can vary. In firefox this outputs a string "Invalid Date" but in chrome it raises an exception.

Edit: Recent versions of Firefox have fixed the behavior to be the same as chrome (raising exception). However, the difference between .toJSON() and .toISOString() remains. (outputting null vs. raising exception)

Mizstik
  • 616
  • 6
  • 5
31

Internally, toJSON() calls toISOString() if it's available, so no difference.

15.9.5.44 Date.prototype.toJSON ( key )

This function provides a String representation of a Date object for use by JSON.stringify (15.12.3).

When the toJSON method is called with argument key, the following steps are taken:

  1. Let O be the result of calling ToObject, giving it the this value as its argument.

  2. Let tv be ToPrimitive(O, hint Number).

  3. If tv is a Number and is not finite, return null.

  4. Let toISO be the result of calling the [[Get]] internal method of O with argument "toISOString".

  5. If IsCallable(toISO) is false, throw a TypeError exception.

  6. Return the result of calling the [[Call]] internal method of toISO with O as the this value and an empty argument list.

smash
  • 326
  • 2
  • 2
  • 1
    That's the answer I was looking for. I was thinking about what's under the hood, but didn't express myself clearly. – Almir Sarajčić Apr 24 '13 at 17:38
  • 6
    Worth noting [`toJSON` is IE8+](https://msdn.microsoft.com/en-US/library/cc907896(v=vs.94).aspx#requirementsTitleToggle) while [`toISOString` is IE9+](https://msdn.microsoft.com/en-us/library/ff925953(v=vs.94).aspx#requirementsTitleToggle) – TheSharpieOne Jun 02 '15 at 21:38
2

JSON date format follows ISO 8601, which is what toISOString produces as well. There is no functional difference between the values returned by either method.

Asad Saeeduddin
  • 46,193
  • 6
  • 90
  • 139
  • 4
    No functional difference, but `toJSON()` is included in JS 1.8.5, while `toISOString()` is included in JS 1.8 (from the MDN docs) – Ian Apr 24 '13 at 17:34
  • 4
    Also (to state the obvious) the names are different, and the `toISOString()` is more appropriate because `toJSON()` implies that there is some part of the JSON spec that defines a datetime format - which there is not. ISO8601 is surely the best thing to do in JSON, but there have been (and continue to be) other formats, such as passing integer number of seconds, milliseconds or ticks since 1970, or [Microsoft's old screwy format](http://stackoverflow.com/q/726334/634824). – Matt Johnson-Pint Apr 24 '13 at 17:48