Skip to content

Instantly share code, notes, and snippets.

@giventofly
Created July 18, 2022 14:20
Show Gist options
  • Save giventofly/4fa5feabbc6005f452e72b31d98e35ec to your computer and use it in GitHub Desktop.
Save giventofly/4fa5feabbc6005f452e72b31d98e35ec to your computer and use it in GitHub Desktop.
javascript unixtime time ago
const timeAgo = (unixtime)=> {
var seconds = Math.floor((new Date() - unixtime * 1000) / 1000);
var interval = seconds / 31536000;
if (interval > 1) {
return Math.floor(interval) + " years ago";
}
interval = seconds / 2592000;
if (interval > 1) {
return Math.floor(interval) + " months ago";
}
interval = seconds / 86400;
if (interval > 1) {
return Math.floor(interval) + " days ago";
}
interval = seconds / 3600;
if (interval > 1) {
return Math.floor(interval) + " hours ago";
}
interval = seconds / 60;
if (interval > 1) {
return Math.floor(interval) + " minutes ago";
}
return Math.floor(seconds) + " seconds ago";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment