Created
September 5, 2022 23:40
-
-
Save bootrino/98281b60f8d855e68444d998fd9bde6a to your computer and use it in GitHub Desktop.
JavaScript human readable date
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
export const timeSince = (timestamp) => { | |
const seconds = Math.floor((new Date() - new Date(timestamp)) / 1000); | |
let interval = Math.floor(seconds / 31536000); | |
if (interval > 1) { | |
return interval + " years"; | |
} | |
interval = Math.floor(seconds / 2592000); | |
if (interval > 1) { | |
return interval + " months"; | |
} | |
interval = Math.floor(seconds / 86400); | |
if (interval > 1) { | |
return interval + " days"; | |
} | |
interval = Math.floor(seconds / 3600); | |
if (interval > 1) { | |
return interval + " hours"; | |
} | |
interval = Math.floor(seconds / 60); | |
if (interval > 1) { | |
return interval + " minutes"; | |
} | |
return Math.floor(seconds) + " seconds"; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment