Created
June 12, 2018 13:26
-
-
Save MarouaneSH/556d5ff4e4f4dae721d5585631167567 to your computer and use it in GitHub Desktop.
Javascript Datediff for human ( CARBON DATEDIFF)
This file contains hidden or 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(date) { | |
var seconds = Math.floor((new Date() - date) / 1000); | |
var 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