Skip to content

Instantly share code, notes, and snippets.

@pvande
Forked from 140bytes/LICENSE.txt
Created May 17, 2011 09:47
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pvande/976212 to your computer and use it in GitHub Desktop.
Save pvande/976212 to your computer and use it in GitHub Desktop.
Calculate the distance in days
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'}
> 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