Created
March 26, 2012 13:56
-
-
Save Victa/2205221 to your computer and use it in GitHub Desktop.
Relative dates
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 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'; | |
| } |
Author
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
You should checkout http://momentjs.com/ ;)