Created
April 25, 2014 08:54
-
-
Save amio/11282675 to your computer and use it in GitHub Desktop.
Angular filter for transforming a date to 'xxx days ago'
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
'use strict'; | |
angular.module('Utils', []) | |
/** | |
* Transform a date to 'xxx days ago' | |
*/ | |
.filter('ago', function () { | |
return function (input) { | |
var date = new Date(input); | |
var seconds = Math.floor((new Date() - date) / 1000); | |
var intervals = { | |
'year': 31536000, | |
'month': 2592000, | |
'day': 86400, | |
'hour': 3600, | |
'minute': 60, | |
'second': 1 | |
}; | |
var counter; | |
for (var intv in intervals) { | |
counter = Math.floor(seconds / intervals[intv]); | |
if (counter > 0) { | |
return counter + ' ' + intv + 's ago'; | |
} | |
} | |
}; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment