Skip to content

Instantly share code, notes, and snippets.

@radelmann
Created October 26, 2015 22:25
Show Gist options
  • Save radelmann/87079dc1f9f9eec35511 to your computer and use it in GitHub Desktop.
Save radelmann/87079dc1f9f9eec35511 to your computer and use it in GitHub Desktop.
return how long ago a date occured in days, hours, or mins.
var getTimeDiff = function(date) {
//input: js date object
//output: returns how long ago the input date occured in a user friendly format
var now = new Date().getTime();
var ms = (now - date.getTime());
var days = Math.round(ms / 86400000); // days
var hrs = Math.round((ms % 86400000) / 3600000); // hours
var mins = Math.round(((ms % 86400000) % 3600000) / 60000); // minutes
if (days > 0) {
return days + ' days ago'
} else if (hrs > 0) {
return hours + ' hours ago'
} else if (mins > 0) {
return mins + ' mins ago'
} else {
return 'just now'
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment