Saturday, 6 June 2015

angularjs date format -- date ordinal suffix



You can not format date like this by angular default date formatter:

 06th Jun 2015 at 9:29


You must implement filter your self:


app.filter('date2', function($filter) {
  var suffixes = ["th", "st", "nd", "rd"];
  return function(input, format) {
    console.clear();
    console.log(input, format);
    var dtfilter = $filter('date')(input, format);
    var day = parseInt($filter('date')(input, 'dd'));
    var relevantDigits = (day < 30) ? day % 20 : day % 30;
    console.log(day, relevantDigits);
    var suffix = (relevantDigits <= 3) ? suffixes[relevantDigits] : suffixes[0];
    return dtfilter.replace('oo', suffix).replace('@', 'at');
    //return dtfilter+suffix;
  };
});



No comments:

Post a comment