Skip to content

Instantly share code, notes, and snippets.

@amitasaurus
Created August 16, 2018 13:48
Show Gist options
  • Save amitasaurus/5854240c59b0197e19034274ac6af138 to your computer and use it in GitHub Desktop.
Save amitasaurus/5854240c59b0197e19034274ac6af138 to your computer and use it in GitHub Desktop.
Angular Filter to display minutes/hours to a time
app.filter('dateFilter', function() {
return function(date) {
let oneSec = 1000;
let oneMin = 60 * oneSec;
let oneHour = 60 * oneMin;
let oneDay = 24 * oneHour;
let now = new Date().getTime();
let transactionTime = new Date(date).getTime();
let transactionDate = new Date(date);
let time = `${(transactionDate.getMonth() + 1).length > 1 ? (transactionDate.getMonth() + 1) : '0' + (transactionDate.getMonth() + 1)}/${transactionDate.getDate()}/${transactionDate.getFullYear()}`;
let diff = now - transactionTime;
if (Math.floor(diff / oneHour) > 24) { //if more than 24Hrs return the exact time
return time
} else {
if (Math.floor(diff / oneHour) > 1) { //if more than a hour return in hours
return `${Math.floor(diff/oneHour)} hours ago`
} else {
return `${Math.floor(diff/oneMin)} mins ago`
}
}
}
})
//USAGE : {{post.time | dateFilter}}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment