Difference between Date.toJSON() and Date.toISOString()

date, javascript

Solution

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:

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

Let tv be ToPrimitive(O, hint Number).

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

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

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

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

Problem

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?

Original source

Related problems