How to handle date fields in JSON complex objects

395 views
Skip to first unread message

AD

unread,
Mar 6, 2013, 7:06:48 AM3/6/13
to ang...@googlegroups.com
Hi,
We are using json  to communicate to server using $http service.One of major demerit in this approach is json data format is not supporting the date datatype.
so when we get an object from server we need to explicitly make those field to Date

$scope.responsefromserver.dateofBirth=new Date($scope.responsefromserver.dateofBirth);

does any body knows workaround for this problem

Thank you

AD

Rob Bygrave

unread,
Mar 6, 2013, 3:37:25 PM3/6/13
to ang...@googlegroups.com

http://docs.angularjs.org/api/ng.filter:date

In case you are not aware angular has a date filter which can format 'date values' and this works on various formats such as unix epoch time and iso8601 (so they don't have to be real date objects to format them). For me this has meant that to date I have needed to convert these it into "real javascript date" when I have needed to do calcuations, present as 'humane dates' like 2 days ago or editing.

So, assuming you know that - no I personally don't know of a workaround I'd suggest. 

Cheers, Rob.

AD

unread,
Mar 10, 2013, 2:58:27 PM3/10/13
to ang...@googlegroups.com
Thank you Rob,
i know about ng-date filter .
Even though my problem is not specific to angularjs .But with introduction of angularjs http is mostly used for object transfer ,rather than rendered screen data.I think angularjs should introduce/reaccomend  some sort of object transfer protocol

Thank you
AD

Sander Elias

unread,
Mar 11, 2013, 1:30:06 AM3/11/13
to ang...@googlegroups.com
Hi AD,

If you need this, you can hook in your own function to transform $http responses. There are many reasons dates are undefined in the JSON format. And or good reason. So I don't think its a good idea try to solve this in angular.
However, if you know your back end, and know how to figure out witch properties needs to be converted to dates, you can write your on function to do that.
And it is easy to hook up this function to your app. There are as usual different way's to solve this.
  1. set up your own $httpProvider.defaults.transformResponse function, this will adapt angular for your whole system.
  2. overide the response locally. do this if you need answers from different origins.
  3. use a decorator on $http, does the same as 1, but easier. A little bit extra overhead tough.
Regards
Sander Elias

AD

unread,
Mar 12, 2013, 1:12:12 PM3/12/13
to ang...@googlegroups.com
Thank you Sander Elias,
we are going with first approach,
But i still feel angularjs can do something to over-cum these issues , like generic conversion of fields of  iso 8601 string to date  .etc

Thanks
AD

Sander Elias

unread,
Mar 12, 2013, 11:18:49 PM3/12/13
to ang...@googlegroups.com
Hi AD,

Well, this not much to do with angular in particular. I don't see a way to create a generic function for this. given a random JSON object, how are you going to tell what's a date and whats not? Even if you only consider ISO 8601, there are quite some different way's do denote a date. 
form the w3 std definition:

<qoute>

The formats are as follows. Exactly the components shown here must be present, with exactly this punctuation. Note that the "T" appears literally in the string, to indicate the beginning of the time element, as specified in ISO 8601.

   Year:
      YYYY (eg 1997)
   Year and month:
      YYYY-MM (eg 1997-07)
   Complete date:
      YYYY-MM-DD (eg 1997-07-16)
   Complete date plus hours and minutes:
      YYYY-MM-DDThh:mmTZD (eg 1997-07-16T19:20+01:00)
   Complete date plus hours, minutes and seconds:
      YYYY-MM-DDThh:mm:ssTZD (eg 1997-07-16T19:20:30+01:00)
   Complete date plus hours, minutes, seconds and a decimal fraction of a
second
      YYYY-MM-DDThh:mm:ss.sTZD (eg 1997-07-16T19:20:30.45+01:00)

</qoute>

Even if you figure this out, you have to parse every string in the JSON to see if it fits. That will take some serious amount off processing power, again and again.

So, I don't think it is a good idea to include this in angular. The hooks are there, so if you need it, you can put it in. 

Regards
Sander Elias
Reply all
Reply to author
Forward
0 new messages