4

There are some posts on this, but not an answer to this specific question.

The server is returning this: "/Date(1304146800000)/"

I would like to not change the server-side code at all and instead parse the date that is included in the .Net generated JSON object. This doesn't seem that hard because it looks like it is almost there. Yet there doesn't seem to be a quick fix, at least in these forums.

From previous posts it sounds like this can be done using REGEX but REGEX and I are old enemies that coldly stare at each other across the bar.

Is this the only way? If so, can someone point me to a REGEX reference that is appropriate to this task?

Regards,

Guido

2 Answers 2

2

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

Here's 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)/");
Sign up to request clarification or add additional context in comments.

11 Comments

Sure, reply if you need help understanding the RegEx, if you just don't care, that's fine too :)
@Juan: actually your RegEx should be: /\/Date\((\d+)(?:-\d+)?\)\//i because that can be returned from the server (the ignore-case switch isn't mandatory of course).
@Guido: I've updated my answer why this solution is bad from the maintainability point of view. Just to let you know and to think on the long run.
@Juan - I am curious about learing RegEx, but don't want to waste your time. If you have a good REGEX reference for my favorites/bookmarks bar I'd add it for next time.
@Robert: As a means of helping me to better understand REGEX could you explain what your alterations to Juan's original REGEX enable? Thanks again.
|
1

Since you're using jQuery I've extended its $.parseJSON() functionality so it's able to do this conversion for you automatically and transparently.

It doesn't convert only .net dates but ISO dates as well. ISO dates are supported by native JSON converters in all major browsers but they work only one way because JSON spec doesn't support date data type.

Read all the details (don't want to copy blog post content here because it would be too much) in my blog post and get the code as well. The idea is still the same: change jQuery's default $.parseJSON() behaviour so it can detect .Net and ISO dates and converts them automatically when parsing JSON data. This way you don't have to traverse your parsed objects and convert dates manually.

How it's used?

$.parseJSON(yourJSONstring, true);

See the additional variable? This makes sure that all your existing code works as expected without any change. But if you do provide the additional parameter and set it to true it will detect dates and convert them accordingly.

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

  1. Because you lower the risk of human factor of forgetting to convert some variable in your object tree (objects can be deep and wide)
  2. Because your code is in development and if you change some server-side part that returns JSON to the client (rename variables, add new ones, remove existing etc.), you have to think of these manual conversions on the client side as well. If you do it automatically you don't have to think (or do anything) about it.

Two top reasons from the top of my head.

When overriding jQuery functionality feels wrong

When you don't want to actually override existing $.parseJSON() functionality you can minimally change 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 setting you will have to override jQuery's existing functionality.

The good thing is also that you don't change the original jQuery library code file. You put this extension in a separate file and use it at your own will. Some pages may use it, others may not. But it's wise to use it everywhere otherwise you have the same problem of human factor with forgetting to include the extension. Just include your extension in some global Javascript file (or master page/template) you may be using.

3 Comments

Thanks for this. Altering the JQuery $ function makes me a little nervous. Which is not to say I should not do it, it's just something I am not used to having to do. (former Java/Java EE developer). Don't get me going on how Javascript freaks me out. ;)
@Guido: You don't actually change the original jQuery file. No! God forbid! You only override its implementation. Most of the code provided in the blog post is taken from original jQuery code. YOu put this override in a separate file, so it's also easier to update jQuery library because you're still using original files. I've actually only added date detection and smartened conversion.
Gotcha. What if I switch to a newer version of JQuery? Would that present a compatibility problem? The nice thing about Juan's solution is it is distinct from the implementation of JQuery itself.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.