Created
August 28, 2013 18:41
-
-
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.
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
| // 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