user2085143
user2085143

Reputation: 4232

Angular/Javascript - Converting Multiple Dates for Complex Object

Retrieving a large JSON data object from an API, a lot of which needs to be rendered for the view.

There are numerous date fields nested throughout the object, all of which are in milliseconds.

These are required to be converted to a date object for the view.

What is the most pragmatic approach to achieve this?

Is it simply to loop over the object and call getTime() on each of the date fields, or is there something a little more cleaner?

Any advice would be much appreciated.

Upvotes: 0

Views: 50

Answers (2)

DennisBeverloo
DennisBeverloo

Reputation: 91

Why not just put them in the view with Angular's date-filter?

<div>{{yourDate | date}}</div>

The filter accepts time in milliseconds (string or number) and can output to a lot of formats.

Upvotes: 0

iamalismith
iamalismith

Reputation: 1571

You can use an angular $http interceptor which to parse and automatically convert your date strings in to date objects.

This tutorial should point you in the right direction http://blog.baltrinic.com/software-development/web-development/angularjs-http-interceptor-date-conversion

Upvotes: 1

Related Questions