Last active
March 1, 2018 06:28
-
-
Save dkomando/4a431e4343d156652e3e to your computer and use it in GitHub Desktop.
AngularJS - $filter - Date Ordinal
This file contains hidden or 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
/* | |
Notes ---------------------------------------------------- | |
Inspired By: | |
http://plnkr.co/edit/v2RuF72A9OPpFj5fvN8A?p=preview | |
My Updated Plunker Of The Inspired Code: | |
http://plnkr.co/edit/rkyc041jmSfzkEbqhcfL?p=preview | |
*/ |
This file contains hidden or 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
// App ------------------------------------------------------------------------- | |
var myApp = angular.module('myApp', []); | |
myApp | |
.controller('myController',myController); | |
myController.$inject = ['$scope','$filter']; | |
function myController($scope,$filter){ | |
$scope.myDate = '2016-02-29'; | |
$scope.myFilteredDate = $filter('setOrdinal')('2016-02-29'); | |
} | |
// Filters --------------------------------------------------------------------- | |
myApp | |
.filter('setOrdinal', setOrdinal); | |
setOrdinal.$inject = ['$filter']; | |
function setOrdinal($filter){ | |
return function (input){ | |
function ordinal(number){ // Function from MomentJS | |
var b = number % 10, | |
output = (parseInt(number % 100 / 10) === 1) ? 'th' : (b === 1) ? 'st' : (b === 2) ? 'nd' : (b === 3) ? 'rd' : 'th'; | |
return number + output; | |
} | |
var date = $filter('date'), | |
// https://docs.angularjs.org/api/ng/filter/date | |
month = date(input,'MMMM'), | |
day = date(input,'d'), | |
year = date(input,'yyyy'); | |
day = ordinal(day); | |
return month+' '+day+', '+year; // Output date however you need it here! | |
}; | |
} |
This file contains hidden or 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
<!DOCTYPE html> | |
<html lang="en" ng-app="myApp"> | |
<head> | |
<meta charset="UTF-8"> | |
<title>Fake Usage Document</title> | |
</head> | |
<body> | |
<div ng-controller="myController"> | |
Cleaned Date: <span ng-bind="myDate | setOrdinal"></span><br> | |
My Pre-filtered Date: <span ng-bind="myFilteredDate"></span> | |
</div> | |
<!-- Angular --> | |
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.9/angular.min.js"></script> | |
<script src="app.js"></script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment