MVC Controller类下面有这样一个方法
// // Summary: // Creates a System.Web.Mvc.JsonResult object that serializes the specified object // to JavaScript Object Notation (JSON). // // Parameters: // data: // The JavaScript object graph to serialize. // // Returns: // The JSON result object that serializes the specified object to JSON format. The // result object that is prepared by this method is written to the response by the // ASP.NET MVC framework when the object is executed. protected internal JsonResult Json(object data);
一般用在Action中返回一个JSON与客户端JS(JQuery)交互。
public JsonResult getRepChangeTest() { return Json(myobj); }
当你的对像里有日期时间字段时,这个Json方法会把它序列化成这种格式
"rec_dt": "/Date(1498466813000)/",
在客户端JS里,我们把它当作字符串处理一下,然后转成js的日期格式,比如
function dt2(tm) { var time = tm.replace(/[\'\"\\\/\b\f\n\r\t)]/g, '').substring(5); console.log(time); //取得时间戳数值 var date = new Date(parseInt(time)); //转成日期格式 //console.log(date); // 下面根据需要转换成你要的格式 var month = date.getMonth() + 1 < 10 ? "0" + (date.getMonth() + 1) : date.getMonth() + 1; var currentDate = date.getDate() < 10 ? "0" + date.getDate() : date.getDate(); var hh = date.getHours() < 10 ? "0" + date.getHours() : date.getHours(); var mm = date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes(); var dt= date.getFullYear() + "-" + month + "-" + currentDate + " " + hh + ":" + mm; alert(dt); }