I've modified WebAPI JSON.Net serialization to use JavaScriptDateTimeConverter. The plan was to get jscript date object instead of ISO 8601 formatted string. But unfortunately when I do post request with jquery like this:
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "#UrlToService",
dataType: "json",
data: JSON.stringify(args),
success: function (data) {
callSucceeded(data);
},
error: function (jqXHR, textStatus, errorThrown) {
BJ.GenericWebAPIFailMethod(jqXHR);
}
});
error: is entered, textStatus is parseerror and errorThrown is giving me this info: SyntaxError: JSON.parse: unexpected keyword return window.JSON.parse( data ); jquery-1.7.2.js (line 564)
Part of my returned json object looks like:
"CreatedOn": Date {Thu Jul 25 2013 12:54:36 GMT+0200 (Central Europe Standard Time)}
"DisplayName": "Toronto, ON, Canada"
"Description": "12:54"
so it is indeed formatting my date as requested, but is fails later on parsing.
Any thoughts how to achieve what I wanted?
p.s. This is how I did that, in global.asax (asp.net web forms):
var formatters = GlobalConfiguration.Configuration.Formatters;
var jsonFormatter = formatters.JsonFormatter;
var settings = jsonFormatter.SerializerSettings;
settings.Converters.Add(new JavaScriptDateTimeConverter());
JavaScriptDateTimeConverterin your Web API? If you leave it as an ISO 8601 date, then after jQuery deserializes the response you can convert the date string to a Date object simply by doingvar date = new Date(data.CreatedOn);.