Last active
August 29, 2015 14:23
-
-
Save tomsmeding/00446ec465f4073714cf to your computer and use it in GitHub Desktop.
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 niceDate(d){ | |
var now=new Date(); | |
if(!d.getFullYear)d=new Date(d); | |
if(d.getDate()==now.getDate()){ | |
if(d.getHours()==now.getHours()){ | |
if(d.getMinutes()==now.getMinutes()){ | |
return now.getSeconds()-d.getSeconds()+" seconds ago"; | |
} | |
return now.getMinutes()-d.getMinutes()+" minutes ago"; | |
} | |
return now.getHours()-d.getHours()+" hours ago"; | |
} | |
var daydiff=~~((now.getTime()-d.getTime())/1000/3600/24); | |
if(daydiff<7){ | |
return ["monday","tuesday","wednesday","thursday","friday","saturday","sunday"][(d.getDay()+6)%7]; | |
} | |
if(daydiff<31){ | |
return daydiff+" days ago"; | |
} | |
if(daydiff<365){ | |
return ~~(daydiff/31)+" months ago"; | |
} | |
return now.getFullYear()-d.getFullYear()+" years ago"; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment