I’ve been working with JSon services quite a bit these days, and one of the big issues with JSon to Javascript translation is the Date element from server side. Since JSon doesn’t return a valid Javascript date object (which in my mind, was a very poor design at the implementation of the JSon form factor), it is up to the developer to make this happen. I have written a simple Javascript function that translates from the JSon format to a user-friendly format here:

 

Code Snippet
  1. function formatDate(dateToFormat){
  2.     var d = new Date(parseInt(dateToFormat.substr(6)));
  3.     var day = d.getDay();
  4.     var m = d.getMonth() + 1;
  5.     var month = (m < 10) ? '0' + m : m;
  6.     var yy = d.getYear();
  7.     var year = (yy < 1000) ? yy + 1900 : yy;
  8.     var hour = d.getHours();
  9.     var min = d.getMinutes();
  10.  
  11.     return (day + "/" + month + "/" + year + " " + hour + ":" + min);
  12. }

 

Javascript usage is as follows:

formatDate(JsonReturnedDate);

 

This function can be modified to fulfill pretty much any date requirement you have, and can also just return a dynamic JS Date object if you would like.

About these ads