Format relative timestamps
Convert dates around an injected clock into stable, threshold-aware past and future labels.Description
Requirements
The solution exports the required formatRelativeTimestamp API from the editable module.
Just now works deterministically for the documented normal, boundary, and repeated-use scenarios.
Past thresholds works deterministically for the documented normal, boundary, and repeated-use scenarios.
Future thresholds works deterministically for the documented normal, boundary, and repeated-use scenarios.
Boundary values works deterministically for the documented normal, boundary, and repeated-use scenarios.
Injected now works deterministically for the documented normal, boundary, and repeated-use scenarios.
Invalid date works deterministically for the documented normal, boundary, and repeated-use scenarios.
Five minutes ago
value 11:55 UTC and injected now 12:00 UTC
5 minutes ago
The injected clock makes the result independent of runtime time and timezone.
Constraints
Require the now argument rather than calling Date.nowUse deterministic English labelsThrow RangeError for invalid date values
Hints
Hint 1
Convert both inputs to epoch milliseconds before selecting a unit.
Hint 2
Choose thresholds from smallest to largest and keep sign separate from magnitude.
Hint 3
Test values exactly on both sides of each unit boundary.