Skip to content

Instantly share code, notes, and snippets.

@Victa
Created March 26, 2012 13:56
Show Gist options
  • Save Victa/2205221 to your computer and use it in GitHub Desktop.
Save Victa/2205221 to your computer and use it in GitHub Desktop.
Relative dates
// Relative times
function prettyDate(date) {
var date, seconds, formats, i = 0, f;
date = new Date(date);
seconds = (new Date - date) / 1000;
formats = [
[60, 'seconds', 1],
[120, '1 minute ago'],
[3600, 'minutes', 60],
[7200, '1 hour ago'],
[86400, 'hours', 3600],
[172800, 'Yesterday'],
[604800, 'days', 86400],
[1209600, '1 week ago'],
[2678400, 'weeks', 604800]
];
while (f = formats[i ++]) {
if (seconds < f[0]) {
return f[2] ? Math.floor(seconds / f[2]) + ' ' + f[1] + ' ago' : f[1];
}
}
return 'A while ago';
}
@unity
Copy link

unity commented Jul 20, 2012

You should checkout http://momentjs.com/ ;)

@Victa
Copy link
Author

Victa commented Jul 20, 2012

Yes, thanks! I'm using it for a while ;)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment