Skip to content

Instantly share code, notes, and snippets.

@lukevella
Last active August 15, 2023 02:34
Show Gist options
  • Save lukevella/f23423170cb43e78c40b to your computer and use it in GitHub Desktop.
Save lukevella/f23423170cb43e78c40b to your computer and use it in GitHub Desktop.
Elapsed Time AngularJS Filter
// {{ dateString | elapsed }}
angular.module('elapsedFilter', [])
.filter('elapsed', function(){
return function(date){
if (!date) return;
var time = Date.parse(date),
timeNow = new Date().getTime(),
difference = timeNow - time,
seconds = Math.floor(difference / 1000),
minutes = Math.floor(seconds / 60),
hours = Math.floor(minutes / 60),
days = Math.floor(hours / 24);
if (days > 1) {
return days + " days ago";
} else if (days == 1) {
return "1 day ago"
} else if (hours > 1) {
return hours + " hours ago";
} else if (hours == 1) {
return "an hour ago";
} else if (minutes > 1) {
return minutes + " minutes ago";
} else if (minutes == 1){
return "a minute ago";
} else {
return "a few seconds ago";
}
}
})
@poojit123
Copy link

Thanks dear

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment