Carlos Pliego Carlos Pliego - 1 year ago 127
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 Source

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}}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download