Created
November 10, 2014 13:22
-
-
Save kalbarczyk/5c7d6d60d59ad529b98d to your computer and use it in GitHub Desktop.
AngularJS - upcoming date filter
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
<!DOCTYPE html> | |
<html data-ng-app="app"> | |
<head> | |
<title>AngularJS - upcoming date filter</title> | |
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.css" /> | |
</head> | |
<body data-ng-controller="defaultCtrl"> | |
<div class="container"> | |
<div data-ng-repeat="date in dates">{{date | upcoming}}</div> | |
</div> | |
<script src="http://code.jquery.com/jquery-1.11.1.js"></script> | |
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js"></script> | |
<script src="https://code.angularjs.org/1.3.0/angular.js"></script> | |
<script> | |
var app = angular.module('app', []); | |
app.controller('defaultCtrl',['$scope','dates', function ($scope, dates) { | |
$scope.dates = dates; | |
}]); | |
app.filter('upcoming', function () { | |
return function (date) { | |
var now = new Date(); | |
var filteredDate = new Date(date); | |
if (filteredDate != 'Invalid Date') { | |
if (now.getTime() < filteredDate.getTime()) { | |
console.log('Wrong date:' + date); | |
} | |
else { | |
return date; | |
} | |
} | |
else { | |
console.log('Invalid Date Format:' + date); | |
} | |
} | |
}); | |
app.factory('dates', function () { | |
return [ | |
'2014-09-15T14:54:46.489Z', | |
'2014-10-15T11:45:25.459Z', | |
'2014-11-10T12:11:33.359Z', | |
'2014-11-10T14:21:43.189Z', | |
'2015-11-15T15:11:23.279Z', | |
'20aaa16-10-18T16:41:53.389Z' | |
]; | |
}); | |
</script> | |
</body> | |
</html> | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment