当前位置:
首页 >
前端技术
> javascript
>内容正文
javascript
JSON.stringify转换Date不正确的解決方法
生活随笔
收集整理的这篇文章主要介绍了
JSON.stringify转换Date不正确的解決方法
小编觉得挺不错的,现在分享给大家,帮大家做个参考.
JSON.stringify转换Date不正确的原因:国际时区(UTC)和中国时区(GMT)的原因,东八区+8等于国际时区。
解决方法,重新Es5的Date.prototype.toJSON方法,代码如下:
function dateFormat(date, fmt) {if (null == date || undefined == date) return '';var o = {"M+": date.getMonth() + 1, //月份"d+": date.getDate(), //日"h+": date.getHours(), //小时"m+": date.getMinutes(), //分"s+": date.getSeconds(), //秒"S": date.getMilliseconds() //毫秒 };if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (date.getFullYear() + "").substr(4 - RegExp.$1.length));for (var k in o)if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));return fmt; }Date.prototype.toJSON = function () { return dateFormat(this,'yyyy-MM-dd')} JSON.stringify(data);
总结
以上是生活随笔为你收集整理的JSON.stringify转换Date不正确的解決方法的全部内容,希望文章能够帮你解决所遇到的问题。
- 上一篇: 火狐最实用的几款插件介绍[含附件]
- 下一篇: CentOS安装运行NodeJS框架Ex