JSON.stringify转换JSON时日期时间不准确的解决方法
作者:junjie 时间:2014-07-20 13:25:07
调用JSON.stringify将对象转为对应的字符串时,如果包含时间对象,时间对象会被转换为国家标准时间(ISO),而不是当前国家区域的时间,测试代码如下:
<script>
//var o = new Date();
//console.log(o.toString())//中国时区时间,格式如“Wed Jun 11 2014 10:51:42 GMT+0800”
//console.log(JSON.stringify(o)); //输出国际标准时间(ISO),减少了8个小时 格式如“2014-06-11T02:51:42.624Z”
</script>
要想JSON.stringify转换日期对象Date返回当前所在国家的时区,而不是国际标准时间,可以重写Date对象的prototype的toJSON方法,返回自定义时间格式,因为JSON.stringify调用的就是Date对象的toJSON方法,示例如下:
<script>
Date.prototype.toJSON = function () { return this.toLocaleString(); }
var o = new Date();
console.log(o.toString())//默认格式:“Wed Jun 11 2014 10:51:42 GMT+0800”
console.log(JSON.stringify(o)); //输出自定义的本地时间:“2014年6月11日 10:57:27”
</script>
标签:JSON.stringify,日期时间
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
关注oblog 关注xml-rpc 自己开发客户端小记
2009-10-04 20:27:00
css设计小巧三条
2008-01-21 13:04:00
SQL Server重温 事务
2012-08-21 10:48:15
浅谈ASP自动采集程序及入库
2007-08-17 11:25:00
使用ASP订制自己的XML文件读写方法
2008-10-24 09:35:00
初学ASP编程易犯的一个错误要注意
2008-11-07 15:08:00
详解ASP图片上传保存到数据库功能
2008-01-25 18:47:00
Check In和Check Out的多人协作管理
2007-02-03 11:39:00
JavaScript解决Joseph问题
2008-06-21 17:11:00
豆瓣的Url结构方式一览
2010-03-01 12:44:00
![](https://img.aspxhome.com/file/UploadPic/20103/1/douban_url-66s.png)
php中fgetcsv()函数用法实例
2023-06-15 05:50:37
数据库自动化技术弥补数据库DBA短缺难题
2009-02-04 16:53:00
ASP提高数据显示效率-缓存探幽
2007-09-28 12:37:00
栅格:从混乱到秩序
2008-07-02 12:09:00
![](https://img.aspxhome.com/file/UploadPic/20087/2/200872121218318s.gif)
学以致用 驳“ASP低能论”
2007-09-30 13:01:00
通用分页存储过程
2007-11-28 13:08:00
一文带你吃透什么是PHP中的序列化
2023-06-12 19:44:20
Oracle 8i字符集乱码问题析及其解决办法
2010-07-26 13:29:00
SNS用户体验和互动性浅析
2011-01-17 17:56:00
![](https://img.aspxhome.com/file/UploadPic/20111/17/01-46s.jpg)
全新感受Oracle 9i
2010-07-16 13:32:00