Search code examples
angularjsdateangular-filters

Angular date filter with date-string


I have a datestring in the following format: YYYY-MM-DD HH:mm:SS

I want to use the Angular date filter in the binding to change it to a YYYY-MM-DD HH:mm format.

I am trying {{data.date | date : "YYYY-MM-DD HH:mm"}}, but it doens't seem to work. Any ideas on what to change to get it to recognize my date format?


Solution

  • Try this

    In Js file

    angular.module('yourmodule').filter('datetime', function($filter)
    {
     return function(input)
     {
      if(input == null){ return ""; } 
    
      var _date = $filter('date')(new Date(input),
                                  'MMM dd yyyy - HH:mm:ss');
    
      return _date.toUpperCase();
    
     };
    });
    

    In HTML

    <span>{{ d.time | datetime }}</span>