Last active
August 15, 2023 02:34
-
-
Save lukevella/f23423170cb43e78c40b to your computer and use it in GitHub Desktop.
Elapsed Time AngularJS 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
// {{ 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"; | |
} | |
} | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks dear