Using .NET JavaScriptSerializer.Deserialize with DateTime from client

json date format
json date converter

I'm using the JavaScriptSerializer.Deserialize<>() method to convert JSON I receive from the client into a custom C# class. One of the properties of that class is a DateTime. Currently the Deserialize<>() method throws an error, saying

"(my date string)" is not a valid value for DateTime.

I've tried sending the date using several different formats, including ticks and other formats produced by the various built-in JavaScript Date() methods, but none of them have worked.

Exactly what format is the Deserialize<>() method expecting in order to parse it into a .NET DateTime?

You are right, @friendlycello. Unfortunally, JSON.stringify() removes backslashes from this \/Date(ticks)\/ .Net serializer DateTime format.

I wrote a custom function that adjusts the output from JSON.stringify(), including these backslashes. So, I can keep almost untoched, only replacing from JSON.stringify() to customJSONstringify() in $.ajax() data: param.

function customJSONstringify(obj) {
    return JSON.stringify(obj).replace(/\/Date/g, "\\\/Date").replace(/\)\//g, "\)\\\/")
}

Watch out for Date Kind in JSON Deserialization, I'm using the JavaScriptSerializer.Deserialize<>() method to convert JSON I receive from the client into a custom C# class. One of the properties of that class is a  One particular area that is frustrating to deal with is the serialization of the C# DateTime object from the server to the client. The DateTime object is converted into a string inside DTOs when serialized into JSON. When it reaches the client, it is still, in fact, a string instead of the Javascript date object. If I want to use any of the

Eduardo provided a solution on the JavaScript side. You also have the option of correcting the issue on the server side.

// C# Side
var obj = Regex.Replace(json, @"/Date\((\-?\d*)\)/", @"\/Date($1)\/")

Note that I used a single replace. This is safer and more accurate than using two replace(). The same expression can be used to replace the expression in the JavaScript example.

// Safer version of function using a single replace
function customJSONstringify(obj) {
    return JSON.stringify(obj).replace(/\/Date\((\-?\d*)\)\//g, "\\/Date($1)\\/");
}

Two calls to replace() could cause the second replace to replace text that had nothing to do with the data. Note to be even safer the expression can be updated to only replace instances of /Date(.\*)/ that are preceded and followed by a single quote. That would ensure that if there was text describing the /Date(.\*)/ syntax it wouldn't get replaced.

JavaScriptSerializer.Deserialize<DateTime , dates that have been sent from the client encoded in JSON format, and you're using [TestMethod] public void DateTest() { DateTime date = DateTime. NET code can make a decision to return the date in any format it chooses but the tend to be easier and more flexible using the JavaScriptSerializer. .NET Framework Deserialization using System.Web.Script.Serialization.JavaScriptSerializer Example The JavaScriptSerializer.Deserialize<T>(input) method attempts to deserialize a string of valid JSON into an object of the specified type <T> , using the default mappings natively supported by JavaScriptSerializer .

Figured it out - it works when in the format \/Date(ticks)\/

Note: if you use JSON.stringify to create you request, it will automatically escape your backslashes, resulting in a parse error on the server side. The only way I found to do this was to replace all instance of \\ with \ on the server side.

Timestamp deserialization problem?, Deserialize(jsonTime); DateTime toLoc = restore JavaScriptSerializer jss = new JavaScriptSerializer(); string jsonTime The client-side serializer Sys. Privacy Statement|; Terms of Use|; Contact Us|; Advertise with Us|  To see more details about the class you can see my previous post: Passing complex objects between client browser and web server using ASP.NET AJAX framework or official ASP.NET AJAX documentation. I have the problem with correct deserialization of DateTime types in my custom serializable classes.

After receving the error

/Date(1347992529530)/ is not a valid value for DateTime.

using the replace suggested by @Luis Perez worked for me.

var data = ko.toJSON({ objext: obj});
$.ajax({
    url: "/API/API.asmx/SaveObject",
    type: "POST",
    dataType: "json",
    contentType: "application/json; char-utf8;",
    data: data.replace(/\/Date/g, "\\\/Date").replace(/\)\//g, "\)\\\/"),
    success: function (r) {},
    error: function (e) {},
    complete: function () {}
});

Performance Tips, I'm using the JavaScriptSerializer.Deserialize<>() method to convert JSON I receive from the client into a custom C# class. One of the properties of that class is a  I'm using the JavaScriptSerializer.Deserialize<>() method to convert JSON I receive from the client into a custom C# class. One of the properties of that class is a DateTime. Currently the Deserialize<>() method throws an error, saying "(my date string)" is not a valid value for DateTime.

JSON Serialization / Deserialization using System.Web.Script , Net API client Timestamp of the saved ticks is not the same as received timestamp, using System. var jss = new JavaScriptSerializer() ; The good news is that .NET provides a very elegant and easy way to implement the desired behavior. Create a class deriving from System.Web.Script.Serialization namespace.JavaScriptSerializer . Override the key methods of that class and plugin your own implementation for any Date format you feel comfortable working with.

Javascriptserializer example, NET is faster than DataContractJsonSerializer and JavaScriptSerializer. Reuse Contract Resolver; Optimize Memory Usage; JsonConverters; Manually Serialize; Benchmarks HttpClient client = new HttpClient(); using (Stream s = client. if (​objectType == typeof(DateTime) || objectType == typeof(DateTimeOffset))  The Deserialize method is equivalent to first using the DeserializeObject method to obtain an object graph and then trying to cast the result to type T. During deserialization, the serializer's current type resolver is referenced, which determines the managed type to use when converting elements that are nested inside arrays and dictionary types.

JavaScriptSerializer.Deserialize Method (System.Web.Script , JSON Serialization / Deserialization using System.Web.Script.Serialization.​JavaScriptSerializer / Essential .NET framework. Recently I was working on a module which include DateTime object. I found some interesting facts about this, which I want to share with my blog readers.I serialized DateTime object with c# JavaScriptSerializer, but when I deserialize the object it give me one day less from the date it get serialize

Comments
  • Hi, [{"ChequeNo":"12","ChequeValue":"333","ChequeDate":"23/10/2018","PendDocStatus":false}], for this i am facing the System.FormatException as 23/10/2018 is not a valid value for DateTime.. Can you help me to resolve it.?
  • Hi, for this data "[{\"ChequeNo\":\"123\",\"ChequeValue\":\"2322\",\"ChequeDate\":\"Date(1573324200000)\",\"PendDocStatus\":false}]", i am facing a System.FormatException Date(1573324200000) is not a valid value for DateTime.. can you help me to resolve it.?