A pretty javascript date

I'm putting this up here for later reference. It's taken from stackoverflow.

pretty_date = start_date => {
  var date = new Date()
  var secs = Math.floor((date.getTime() - start_date.getTime()) / 1000);
  if (secs < 60) return secs + " sec(s) ago";
  if (secs < 3600) return Math.floor(secs / 60) + " min(s) ago";
  if (secs < 86400) return Math.floor(secs / 3600) + " hour(s) ago";
  if (secs < 604800) return Math.floor(secs / 86400) + " day(s) ago";
  return date.toDateString();
}

It returns 3 minute(s) ago, 6 days ago etc.

javascript

Edit on github
comments powered by Disqus
Click me