Sheepy99 Sheepy99 -3 years ago 103
JSON Question

Angular - parsing controller variable for binding

I have a controller which call a service and gets back some data, the rough format of which is obj.id, obj.email, obj.date etc....
My date is being returned as 2017-08-14T16:52:24.000Z after using

var d = new Date();
var currentTimeDate = d.toLocaleString();
(Funnily enough if I use this code, I get a clean date string, but after writing to a Mongoose collection and reading later, it's returned like so


When using an ng-repeat directive to iterate through the returned list of objects, i'd like to clean up the date property.
Rather than doing a

obj[index].date = obj[index].date.slice(

//returns 2017-08-14
obj[index].date.substr(0,obj[index].date.indexOf(T)) ,

//returns 16:52:24
obj[index].date.substr(obj[index].date.indexOf(T+1) , obj[index].date.indexOf('.')

)


snippet inside my controller, is there a way to do this in my html view during the ng-repeat procedure whereby I could write something like:

<div>
{{obj.date = 'string building done here'}}
</div>

Answer Source

You can filter <div> {{obj.date | date:'YYYY-MM-DD'}}</div>

or in the controller $filter('date')(obj.date, 'YYYY-MM-DD');

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download