Skip to content

Instantly share code, notes, and snippets.

@arodu
Forked from patrickmooney/time ago in vanilla js
Created January 22, 2019 17:15
Show Gist options
  • Save arodu/1fe30d0ca6a62ec4e67ff52343a789fa to your computer and use it in GitHub Desktop.
Save arodu/1fe30d0ca6a62ec4e67ff52343a789fa to your computer and use it in GitHub Desktop.
function timeSince(timeStamp) {
var now = new Date(),
secondsPast = (now.getTime() - timeStamp.getTime()) / 1000;
if(secondsPast < 60){
return parseInt(secondsPast) + 's';
}
if(secondsPast < 3600){
return parseInt(secondsPast/60) + 'm';
}
if(secondsPast <= 86400){
return parseInt(secondsPast/3600) + 'h';
}
if(secondsPast > 86400){
day = timeStamp.getDate();
month = timeStamp.toDateString().match(/ [a-zA-Z]*/)[0].replace(" ","");
year = timeStamp.getFullYear() == now.getFullYear() ? "" : " "+timeStamp.getFullYear();
return day + " " + month + year;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment