Format Relative Times
Write the code, see it run, pass the checks.
The problem
- medium
- Code
Write timeAgo(date, now), which describes date relative to now in words, as feeds and comment threads do. Each may be a Date or a number of milliseconds, such as Date.now() returns. If they are less than a minute apart, either way, return "just now". Otherwise use the largest unit that fits in the gap, from year (365 days), month (30 days), week (7 days), day, hour and minute, count the whole units, rounding down, and write the count and the unit, with an s when the count isn't 1. A date before now ends in "ago", such as "3 minutes ago" or "1 day ago", and a date after now starts with "in", such as "in 2 weeks".
Your code must pass
- Says just now within a minute
- Counts minutes, hours and days in the past
- Uses weeks, months and years
- Rounds down to whole units
- Talks about the future with in
- Accepts Date objects