Skip to content

Instantly share code, notes, and snippets.

@devguydavid
Created August 28, 2013 18:41
Show Gist options
  • Save devguydavid/6369662 to your computer and use it in GitHub Desktop.
Save devguydavid/6369662 to your computer and use it in GitHub Desktop.
Returns the day within which a given timestamp falls in human-friendly terms.
// Given a timestamp in ms, return the appropriate choice of the following depending on where the timestamp falls:
// X days ago
// yesterday
// today
// tomorrow
// X days from now
function fuzzyDays(timestamp) {
var fuzz = "today";
var todayStartMs = Utils.DayStart(new Date()).getTime();
var dayLengthMs = 24 * 60 * 60 * 1000;
var daysOff = Math.floor((timestamp - todayStartMs) / dayLengthMs);
if (daysOff == 1) fuzz = "tomorrow";
else if (daysOff > 1) fuzz = daysOff.toString() + " days from now";
else if (daysOff == -1) fuzz = "yesterday";
else if (daysOff < -1) fuzz = daysOff.toString().substring(1) + " days ago";
return fuzz;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment