Carlos Pliego Carlos Pliego - 4 months ago 25
AngularJS Question

AngularJs filter sql timestamp "yyyy-MM-dd HH:mm:ss" to something else

I've tried the angular docs of {{someDate | date:'params'}}, however; This doesn't work if someDate is in this format yyyy-MM-dd HH:mm:ss.

How would I convert yyyy-MM-dd HH:mm:ss to say just yyyy using angularjs.

Answer

The date filter is designed to handle multiple input types including date objects and common date strings. If your date string is not recognized by the provided date filter, I would just write my own filter. It would look something like this...

myapp.filter('toYear', function () {
     return function (dateString) {
         var dateObject = new Date(dateString);
         return dateObject.getFullYear();
     };
});

You can use it like this...

{{someDate | toYear}}
Comments