JSON.stringify转换Date不正确的解決方法
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JSON.stringify转换Date不正确的解決方法相关的知识,希望对你有一定的参考价值。
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 hh:mm:ss')}
以上是关于JSON.stringify转换Date不正确的解決方法的主要内容,如果未能解决你的问题,请参考以下文章
“JSON.parse”和“JSON.stringify”有啥区别?
IE8中JSON.stringify方法对自动转换unicode字符的解决方案