Time Sice
by Puni Charana
JavaScript
function timeSince(timeStamp) {
var now = new Date(),
secondsPast = (now.getTime() - timeStamp.getTime()) / 1000;
if (secondsPast < 60) {
return parseInt(secondsPast) < 2 ? '1 sec ago' : parseInt(secondsPast) + ' secs ago';
}
if (secondsPast < 3600) {
return parseInt(secondsPast / 60) < 2 ? '1 min ago' : parseInt(secondsPast / 60)+ ' mins ago';
}
if (secondsPast <= 86400) {
return parseInt(secondsPast / 3600) < 2 ? '1 hr ago' : parseInt(secondsPast / 3600) + ' hrs ago';
}
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;
}
}
d = new Date(Date.now() - (1000 * 60 * 60 * 24 * 30 * 12* 5));
console.log(timeSince(d))