formatWhen()function
formatWhen(target: PossibleDate, current?: PossibleDate, options?: UnitFormatOptions, caller: AnyCaller = formatWhen): string
| Param | Type | |
|---|---|---|
target | PossibleDate | Date the duration is measured to. required |
current | PossibleDate | Date the duration is measured from (defaults to now). |
options | UnitFormatOptions | Formatting options for the chosen duration unit. |
.one | string | String for one of this thing, e.g. product or item or sheep- Used for unitDisplay: "long" formatting.- Defaults to unit reference, e.g. "minute" readonly |
.many | string | String for several of this thing, e.g. products or items or sheep- Used for unitDisplay: "long" formatting.- Defaults to one + "s" readonly |
.abbr | string | Abbreviation for this thing, e.g. products or items or sheep (defaults to one + "s").- Used for unitDisplay: "narrow" formatting.- Defaults to unit reference, e.g. "minute" readonly |
caller | AnyCaller | Function to attribute a thrown error to (defaults to formatWhen itself). Defaults to formatWhen |
| Return | |
|---|---|
string | Compact string like in 10d, 2h ago, or just now. |
| Throws | |
|---|---|
RequiredError | If target or current cannot be converted to a valid date. |
Format a compact best-fit description of when a date happens or happened, e.g. in 10d or 2h ago or in 1w or just now
- See
getBestDurationUnit()for details on how the best-fit unit is chosen. - But: anything under 30 seconds will show
just now, which makes more sense in most UIs.
Examples
formatWhen("2099-01-01") // "in 73 years"