网站开发沟通,网站推广关键词工具,网站建设 课程 说明,做网站赚钱平台JSON.stringify转换Date不正确的原因#xff1a;国际时区#xff08;UTC#xff09;和中国时区#xff08;GMT#xff09;的原因#xff0c;东八区8等于国际时区。
解决方法#xff0c;重新Es5的Date.prototype.toJSON方法#xff0c;代码如下#xff1a; function d…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);