Created
September 25, 2014 13:06
-
-
Save ruffrey/002e5234aa6d355224f1 to your computer and use it in GitHub Desktop.
An Angular filter for outputting the date suffix - th, st, nd, rd
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
}]); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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