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
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.