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);
        }
View Code

相关文章:

  • 2021-05-19
  • 2022-02-09
  • 2022-12-23
  • 2022-01-05
  • 2021-09-20
  • 2021-10-31
  • 2022-12-23
  • 2022-01-09
猜你喜欢
  • 2021-05-26
  • 2021-10-16
  • 2022-12-23
  • 2022-12-23
  • 2021-08-08
  • 2022-01-21
相关资源
相似解决方案