ASP.NET MVC JsonResult Date Format

前端 未结 25 3754
渐次进展
渐次进展 2020-11-21 11:22

I have a controller action that effectively simply returns a JsonResult of my model. So, in my method I have something like the following:

return new JsonRes         


        
25条回答
  •  滥情空心
    2020-11-21 11:53

    Here's some JavaScript code I wrote which sets an value from a date passed from ASP.NET MVC.

    var setDate = function(id, d) {
      if (d !== undefined && d !== null) {
        var date = new Date(parseInt(d.replace("/Date(", "").replace(")/", ""), 10));
        var day = ('0' + date.getDate()).slice(-2);
        var month = ('0' + (date.getMonth() + 1)).slice(-2);
        var parsedDate = date.getFullYear() + "-" + (month) + "-" + (day);
        $(id).val(parsedDate);
      }
    };
    

    You call this function like so:

    setDate('#productCommissionStartDate', data.commissionStartDate);
    

    Where commissionStartDate is the JSON date passed by MVC.

提交回复
热议问题