Created
May 3, 2013 20:52
-
-
Save stamat/5514031 to your computer and use it in GitHub Desktop.
Twitter-like fancy time
This file contains 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
function fancyDate(then, now, suffix) { | |
if(now === undefined) | |
now = new Date(); | |
if(suffix === undefined) | |
suffix = 'ago'; | |
var thenMs = null; | |
typeof then === 'number' ? thenMs = then : thenMs = then.getTime(); | |
var nowMs = null; | |
typeof now === 'number' ? nowMs = now : nowMs = now.getTime(); | |
var passed = Math.round((nowMs - thenMs) / 1000); | |
if (passed > 59) { | |
passed = Math.round(passed / 60); | |
if (passed > 59) { | |
passed = Math.round(passed / 60); | |
if (passed > 23) { | |
if ((now.getFullYear() - then.getFullYear() > 0) | |
&& (12 + now.getMonth() - then.getMonth() > 11)) { | |
return then.getDate() + ' ' | |
+ _months[then.getMonth()] + ' ' | |
+ then.getFullYear(); | |
} else { | |
return then.getDate() + ' ' +_months[then.getMonth()]; | |
} | |
} else { | |
return passed + 'h '+suffix; | |
} | |
} else { | |
return passed + 'm '+suffix; | |
} | |
} else { | |
return passed + 's '+suffix; | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment