javascript – Date.toJSON()和Date.toISOstring()之间的区别

前端之家收集整理的这篇文章主要介绍了javascript – Date.toJSON()和Date.toISOstring()之间的区别前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在检查如何以以下格式显示 JavaScript日期:YYYY-MM-DDTHH:mm:ss.sssZ,但是我看到了两种方法.toJSON().toISOstring().
他们之间有什么真正的区别吗?

解决方法

在内部,toJSON()调用到ISOString()如果可用,所以没有区别.

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.

猜你在找的JavaScript相关文章