Skip to content

Instantly share code, notes, and snippets.

@tomsmeding
Last active August 29, 2015 14:23
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 tomsmeding/00446ec465f4073714cf to your computer and use it in GitHub Desktop.
Save tomsmeding/00446ec465f4073714cf to your computer and use it in GitHub Desktop.
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