Created
July 18, 2022 14:20
-
-
Save giventofly/4fa5feabbc6005f452e72b31d98e35ec to your computer and use it in GitHub Desktop.
javascript unixtime time ago
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
const timeAgo = (unixtime)=> { | |
var seconds = Math.floor((new Date() - unixtime * 1000) / 1000); | |
var interval = seconds / 31536000; | |
if (interval > 1) { | |
return Math.floor(interval) + " years ago"; | |
} | |
interval = seconds / 2592000; | |
if (interval > 1) { | |
return Math.floor(interval) + " months ago"; | |
} | |
interval = seconds / 86400; | |
if (interval > 1) { | |
return Math.floor(interval) + " days ago"; | |
} | |
interval = seconds / 3600; | |
if (interval > 1) { | |
return Math.floor(interval) + " hours ago"; | |
} | |
interval = seconds / 60; | |
if (interval > 1) { | |
return Math.floor(interval) + " minutes ago"; | |
} | |
return Math.floor(seconds) + " seconds ago"; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment