Skip to content

Instantly share code, notes, and snippets.

@kolber
Forked from rails/gist:58761
Created August 20, 2010 06:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kolber/539720 to your computer and use it in GitHub Desktop.
Save kolber/539720 to your computer and use it in GitHub Desktop.
// Ruby strftime: %b %d, %Y %H:%M:%S GMT
time_ago_in_words = function(from, to) {
var to = to ? new Date(to) : new Date,
from = new Date(from);
var seconds = ((to - from) / 1000),
minutes = (seconds / 60);
if (minutes == 0) return 'less than a minute ago';
if (minutes == 1) return 'a minute ago';
if (minutes < 45) return minutes.toFixed() + ' minutes ago';
if (minutes < 90) return 'about 1 hour ago';
if (minutes < 1440) return 'about ' + (minutes / 60).toFixed() + ' hours ago';
if (minutes < 2880) return '1 day ago';
if (minutes < 43200) return (minutes / 1440).toFixed() + ' days ago';
if (minutes < 86400) return 'about 1 month ago';
if (minutes < 525960) return (minutes / 43200).toFixed() + ' months ago';
if (minutes < 1051199) return 'about 1 year ago';
return 'over ' + (minutes / 525960).toFixed() + ' years ago';
};
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<script src="./gistfile1.js"></script>
</head>
<body></body>
<script>
var html = '',
sixteen_days_ago = (new Date() - 1440000000);
html += time_ago_in_words(sixteen_days_ago);
html += "<br>";
html += time_ago_in_words("Jan 15, 2010 15:45:00 GMT");
html += "<br>";
html += time_ago_in_words(1280847213591);
html += "<br>";
html += time_ago_in_words(1280847213591, 1292272648833);
document.querySelector('body').innerHTML = html;
</script>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment