Parsing Automatically Creating a .NET Date Object with Javascript / JQuery

There are several posts on this issue, but no answer to this specific question.

The server returns this: "/Date(1304146800000)/"

I would not change the server-side code at all and instead parse the date that is included in the .Net JSON object created. It doesn't seem so difficult because it looks like he's almost there. However, it seems that it is not so fast, at least in these forums.

From previous posts, this sounds like it can be done with REGEX, but REGEX and I are old enemies who coldly look at each other through the panel.

Is this the only way? If so, can someone point me to the REGEX link appropriate to this task?

Hello,

Guido

+4
source share
2 answers

The link from Robert is good, but we should strive to answer this question here, and not just post links.

Here is a quick function that does what you need. http://jsfiddle.net/Aaa6r/

 function deserializeDotNetDate(dateStr) { var matches = /\/Date\((\d*)\)\//.exec(dateStr); if(!matches) { return null; } return new Date( parseInt( matches[1] ) ); } deserializeDotNetDate("/Date(1304146800000)/"); 
+1
source

Since you are using jQuery, I have expanded the functionality of $.parseJSON() so that it can do this conversion for you automatically and transparently .

It does not convert only .net dates, but also ISO dates. ISO dates are supported by built-in JSON converters in all major browsers, but they only work in one way, since the JSON specification does not support the date data type.

Read all the details (don't want to copy the contents of the blog post because that would be too much) to my blog post and get the code as well. The idea remains the same: change the default jQuery behavior of $.parseJSON() so that it can detect .Net and ISO dates and automatically convert them when parsing JSON data. Thus, you do not need to go through your analyzed objects and convert dates manually.

How is it used?

 $.parseJSON(yourJSONstring, true); 

See the extra variable? This ensures that all your existing code works as expected without any changes. But if you provide an additional parameter and set it to true , it will determine the dates and convert them accordingly.

Why is this solution better than manual conversion? (proposed by Juan)

  • Because you lower the risk of the human forgetting factor in order to transform some variable into your tree of objects (objects can be deep and wide). A.
  • Since your code is under development and if you change the part of the server that returns JSON to the client (rename variables, add new ones, delete existing ones, etc.), you should consider these manual conversions on the client side as well. If you do this automatically, you do not need to think (or do anything) about it.

Two main reasons from the head.

Overriding jQuery functionality erroneously

If you don’t want to actually override the existing $.parseJSON() functions, you can minimize the code and rename the extension to $.parseJSONwithdates() , and then always use your own function when parsing JSON. But you may have a problem when you set your Ajax calls to dataType: "json" , which automatically calls the original parser. If you use this parameter, you will have to override the existing jQuery functions.

It’s also good that you don’t modify the jQuery library source file. You add this extension to a separate file and use it as you wish. Some pages may use it, others may not. But it’s wise to use it everywhere, otherwise you have the same problem of the human factor, forgetting to include the extension. Just add the extension to some global Javascript file (or main page / template) that you can use.

+1
source

Source: https://habr.com/ru/post/1348000/


All Articles