将对象序列化成字符串 时间格式化的两种方式

前言,可忽略:今天在写一个项目的时候,就是先通过id获取后端的数据,然后展示在后台,修改了之后再保存,但是哎呀我去,一直报错显示对应条数不正确,然后我就调试,最终发现,有一项日期相关的数据没有传到后端,再一检查,我明明传了呀,为什么没有传递过去呢?再一调试,发现原来是后端发送过来以后的日期格式因为使用了easyUI之后再传递回去,就会和之前的日期格式不一致,所以就不会接受到这条数据,所以就出错了,嗯,至于怎么传递数据的,请参考我的这篇博客:我使用的是第三种

https://www.cnblogs.com/wangjinya/p/10626887.html

1. 前端js封装转换。

//将序列化成json格式后日期(毫秒数)转成日期格式
function ChangeDateFormat(cellval) {
var date = new Date(parseInt(cellval.replace("/Date(", "").replace(")/", ""), 10));
var month = date.getMonth() + 1 < 10 ? "0" + (date.getMonth() + 1) : date.getMonth() + 1;
var currentDate = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();
var getHours = date.getHours() < 10 ? "0" + date.getHours() : date.getHours();
var getMinutes = date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes();
var getSeconds = date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds();
return date.getFullYear() + "-" + month + "-" + currentDate + ":" + getHours + ":" + getMinutes + ":" + getSeconds;
}

调用的时候,只需要 : ChangeDateFormat(data.SubTime)

2. 后端转换的方式: 

1 //将序列化成json格式后日期(毫秒数)转成日期格式
2         function ChangeDateFormat(cellval) {
3             var date = new Date(parseInt(cellval.replace("/Date(", "").replace(")/", ""), 10));
4             var month = date.getMonth() + 1 < 10 ? "0" + (date.getMonth() + 1) : date.getMonth() + 1;
5             var currentDate = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();
6             return date.getFullYear() + "-" + month + "-" + currentDate;
7         }

调用方式:  return Utility.JsonSerailizeHelper.SerializeObject(easyuiTable, "yyyy-MM-dd");

原文地址:https://www.cnblogs.com/wangjinya/p/10749304.html