Date.toJSON()和Date.toISOstring()之间的区别 [英] Difference between Date.toJSON() and Date.toISOstring()

查看:245
本文介绍了Date.toJSON()和Date.toISOstring()之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在检查如何以以下格式显示JavaScript日期: YYYY-MM-DDTHH:mm:ss.sssZ ,但是我看到了两种方法: a href = http://www.w3schools.com/jsref/jsref_tojson.asp> .toJSON()和 .toISOstring()
它们之间是否有真正的区别?

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?

推荐答案

内部, toJSON()调用 toISOString()(如果可用),所以没有区别。

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


< h2> 15.9.5.44 Date.prototype.toJSON(key)

此函数提供JSON.stringify(15.12.3)使用的Date对象的String表示形式。

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).

使用参数键调用toJSON方法时,将执行以下步骤:

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


  1. 让O为调用ToObject的结果,并为其赋予此值作为其参数。

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

让电视成为ToPrimitive(O,hint

Let tv be ToPrimitive(O, hint Number).

如果tv是一个数字并且不是有限的,则返回null。

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

让toISO是使用参数 toISOString调用O的[[Get]]内部方法的结果。

如果IsCallable(toISO)为false,则抛出TypeError异常。

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

返回调用toISO的[[Call]]内部方法的结果,以O作为此值和一个空参数列表。


这篇关于Date.toJSON()和Date.toISOstring()之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆