Skip to content

Instantly share code, notes, and snippets.

@ruffrey
Created September 25, 2014 13:06
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ruffrey/002e5234aa6d355224f1 to your computer and use it in GitHub Desktop.
Save ruffrey/002e5234aa6d355224f1 to your computer and use it in GitHub Desktop.
An Angular filter for outputting the date suffix - th, st, nd, rd
var app = angular.module('app', []);
app.filter('dateSuffix', function ($filter) {
var suffixes = ["th", "st", "nd", "rd"];
return function (input) {
var dtfilter = $filter('date')(input, 'dd');
var day = parseInt(dtfilter, 10);
var relevantDigits = (day < 30) ? day % 20 : day % 30;
var suffix = (relevantDigits <= 3) ? suffixes[relevantDigits] : suffixes[0];
return suffix;
};
});
app.controller('Ctrl', ['$scope', function ($scope) {
$scope.theDate = new Date();
}]);
<span>{{ theDate | date:'MMMM dd' }}{{ theDate | dateSuffix }}</span>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment