7

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?

5 Answers 5

8

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, "\)\\\/") } 
Sign up to request clarification or add additional context in comments.

1 Comment

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

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.

Comments

1

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.

1 Comment

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

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 () {} }); 

Comments

0

I would suggest using JsonConvert.DeserializeObject<> instead.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.