Skip to content

Instantly share code, notes, and snippets.

@ayushgp
Created October 16, 2015 19:27
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 ayushgp/fd7e62555f75a0e12ac3 to your computer and use it in GitHub Desktop.
Save ayushgp/fd7e62555f75a0e12ac3 to your computer and use it in GitHub Desktop.
var relativeDate = function(date){
var month = ["January", "February", "March", "April", "May", "June","July", "August", "September", "October", "November", "December"];
var now = new Date();
if(date === now){
return "Just now";
}
var yearDiff = date.getYear()-now.getYear();
var monDiff = date.getMonth()-now.getMonth();
var dateDiff = date.getDate()-now.getDate();
if(date.toDateString() === now.toDateString())
return sameDay(date);
else if(yearDiff){
return date.getDate()+", "+month[date.getMonth()]+" "+date.getYear();
}
else if(monDiff){
return date.getDate()+" "+month[date.getMonth()];
}
else if(Math.abs(dateDiff) == 1){
return "Yesterday";
}
else{
return dateDiff+" days ago";
}
};
var sameDay = function(date){
var now = new Date();
console.log(date,now);
if(Math.abs(date.getHours()-now.getHours())>1){
return date.getHours()-now.getHours()+" hours ago";
}
else if(Math.abs(date.getHours()-now.getHours())==1 && Math.abs(date.getMinutes()-now.getMinutes())>60){
return "1 Hour ago";
}
else{
if(Math.abs(date.getMinutes()-now.getMinutes())>1){
return Math.abs(date.getMinutes()-now.getMinutes())+" minutes ago";
}
else if(Math.abs(date.getMinutes()-now.getMinutes())==1 && Math.abs(date.getSeconds()-now.getSeconds())>60){
return "1 minute ago";
}
else{
return Math.abs(date.getSeconds()-now.getSeconds())+" seconds ago";
}
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment