Microsoft uses JavaScriptSerializer to serialize / desirably ASP.NET MVC data. If the format is /Date(utcDate)/ for the Date data type. Try using
'"\\/Date(' + myStory.Deadline.getTime() + ')\\/"'
or
var d = myStory.Deadline; var dateForMS = '"\\/Date(' + Date.UTC (d.getUTCFullYear(), d.getUTCMonth(), d.getUTCDate(), d.getUTCHours(), d.getUTCMinutes(), d.getUTCSeconds(), d.getUTCMilliseconds()) + ')\\/"'
You can also just use the Sys.Serialization.JavaScriptSerializer from MicrosoftAjax.js to serialize Deadline or any other type of Date .
UPDATED . You should probably use '\/Date(' and ')\/' instead of '"\\/Date(' and ')\\/"' . It all depends on where you insert the line.
UPDATED 2 : Now I have it! ASP.NET MVC is mainly used for Ajax publishing form fields. On the server side, only the Parse method for each type will be used to convert the published parameter to type. Thus, you can use any string format that is supported by DateTime.Parse . For example, you can use the ISO 8601 format, for example, '2010-08-29T13: 15: 00.0000000Z'. To do this, in modern browsers (firefox, chrome), you can use the toISOString() function. To be more independent, you can implement data conversion as described in http://williamsportwebdeveloper.com/cgi/wp/?p=503 :
var d = new Date($('#story-deadline').val()) //var d = new Date(); // get the date. Here we use just Now. var dAsISOString; if ($.isFunction(d.toISOString)) { //alert("internal toISOString are used!"); dAsISOString = d.toISOString(); } else { dAsISOString = d.getUTCFullYear() + '-' + padzero(d.getUTCMonth() + 1) + '-' + padzero(d.getUTCDate()) + 'T' + padzero(d.getUTCHours()) + ':' + padzero(d.getUTCMinutes()) + ':' + padzero(d.getUTCSeconds())+'.'+ pad2zeros(d.getUTCMilliseconds()) + 'Z'; } var myStory = { Summary: 'Test description', Size: 8, Dedline: dAsISOString }; $.ajax({ url: '/Project/1/AddStory', data: { Summary: myStory.Summary, Size: myStory.Size, Dedline: myStory.Dedline }, dataType: 'json', // ... });
Oleg
source share