Nathan Friend Nathan Friend - 6 months ago 70
Javascript Question

Using .NET JavaScriptSerializer.Deserialize with DateTime from client

I'm using the

method to convert JSON I receive from the client into a custom C# class. One of the properties of that class is a
. Currently the
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
method expecting in order to parse it into a .NET


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, "\)\\\/")