Last active
June 17, 2023 22:07
-
-
Save patrickmooney/7060259d11ac6281ce1c 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 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