简体   繁体   English

JSON.stringify返回日期的不同值

[英]JSON.stringify returns different values for dates

i'm needing my json.stringify method to return me something like this: 我需要我的json.stringify方法来返回这样的东西:

//The code below is the same as JSON.stringify(new Date());
console.log(new Date().toJSON());

This returns me the following: 这让我想到以下内容:

"/Date(1373046760480-0300)/"

which is fine, but it happens in certain scenarios that the same operation returns me this: 这很好,但它发生在某些情况下,相同的操作返回我:

"2013-07-05T17:52:55.434Z"

which is not the expected result, and then i have to create string to have the expected result. 这不是预期的结果,然后我必须创建字符串以获得预期的结果。

Does anybody know why this happens? 有人知道为什么会这样吗?

Unless someone accidentally (by using third party library) overwrites Date.prototype.toJSON the default output of JSON date is the latter: 除非偶然(通过使用第三方库)覆盖Date.prototype.toJSON JSON日期的默认输出是后者:

Here's what could be (a non-standard implementation used by asp.net) 这是可能的(asp.net使用的非标准实现)

Date.prototype.toJSON = function(){ return "/Date(" + this.getTime() + ")/" }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM