Skip to content

Instantly share code, notes, and snippets.

@pwndev
Last active September 18, 2021 22:47
Show Gist options
  • Save pwndev/3575a3c07b3e79bfc8d913363ec0bb4b to your computer and use it in GitHub Desktop.
Save pwndev/3575a3c07b3e79bfc8d913363ec0bb4b to your computer and use it in GitHub Desktop.
"Advanced" time formatting
timeSincePost(date) {
let diff = Math.floor((new Date() - date) / 1000);
if (diff <= 1) return "just now";
if (diff < 20) return diff + " seconds ago";
if (diff < 60) return "less than a minute ago";
if (diff <= 3540) return Math.round(diff / 60) + " min ago";
if (diff <= 5400) return "1 hour ago";
if (diff <= 86400) return Math.round(diff / 3600) + " hours ago";
if (diff <= 129600) return "1 day ago";
if (diff < 604800) return Math.round(diff / 86400) + " days ago";
if (diff <= 777600) return "1 week ago";
return "on " + this.post.date;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment