Skip to content

Instantly share code, notes, and snippets.

@fmal
Created November 25, 2017 22:43
Show Gist options
  • Select an option

  • Save fmal/2390ed07bc87306895c7d33a29d43126 to your computer and use it in GitHub Desktop.

Select an option

Save fmal/2390ed07bc87306895c7d33a29d43126 to your computer and use it in GitHub Desktop.
export default function humanReadableTimeDiff(date) {
var dateDiff = Date.now() - date;
if (dateDiff <= 0 || Math.floor(dateDiff / 1000) == 0) {
return 'now';
}
if (dateDiff < 1000 * 60) {
return Math.floor(dateDiff / 1000) + 's';
}
if (dateDiff < 1000 * 60 * 60) {
return Math.floor(dateDiff / (1000 * 60)) + 'm';
}
if (dateDiff < 1000 * 60 * 60 * 24) {
return Math.floor(dateDiff / (1000 * 60 * 60)) + 'h';
}
return Math.floor(dateDiff / (1000 * 60 * 60 * 24)) + 'd';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment