[英]How do i tell javascript to not convert date object to UTC while serializing
我想要
JSON.stringify(new Date());
返回本地日期。 我怎样才能做到这一点?
处理这种情况最好的办法是写一个替代品的功能,并传递给JSON.stringify
。 替换程序将检测日期并输出所需的格式。
JSON.stringify(new Date(), function(key, value) {
var rawValue = this[key];
if (rawValue instanceof Date) {
return /*...whatever format you want using `rawValue`...*/;
}
return value;
});
我在那里做了一个内联函数,但是您当然可以将它设为重用的命名函数。
例:
console.log(JSON.stringify(new Date(), function(key, value) { var rawValue = this[key]; if (rawValue instanceof Date) { return "Your string here for " + rawValue; } return value; }));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.