-
-
Save pvande/976212 to your computer and use it in GitHub Desktop.
Calculate the distance in days
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(d,n){d=(n=(d=~~((new Date-d)/8.64e7))<0)?-d:d;return d?d+' day'+(d>1?'s ':' ')+['ago','from now'][n+0]:'today'} |
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
> DAYS = 1000 * 60 * 60 * 24 | |
> y = function(d,n){d=(n=(d=~~((new Date-d)/8.64e7))<0)?-d:d;return d?d+' day'+(d>1?'s ':' ')+['ago','from now'][n+0]:'today'} | |
> [-3, -2, -1.5, -1, -0.5, 0, 0.5, 1, 1.5, 2, 3].forEach(function(n) { console.log(y(Date.now() + n * DAYS)) }) | |
3 days ago | |
2 days ago | |
1 day ago | |
1 day ago | |
today | |
today | |
today | |
1 day from now | |
1 day from now | |
2 days from now | |
3 days from now |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment