Calculates the difference between two dates, returning the value in the largest unit appropriate. For control over the unit, use dateDiff.
Returns values that can be passed to Intl.RelativeTimeFormat or Intl.NumberFormat
Intl.RelativeTimeFormat
Intl.NumberFormat
The date time to calculate the relative time for.
The date to compare to. Defaults to the current time.
The options for the calculation.
const [value, unit] = dynamicDateDiff(someDate);const formatter = new Intl.RelativeTimeFormat("en-US", { style: "long" });const formatted = formatter.format(value, unit); // e.g. "in 5 days", "3 weeks ago" Copy
const [value, unit] = dynamicDateDiff(someDate);const formatter = new Intl.RelativeTimeFormat("en-US", { style: "long" });const formatted = formatter.format(value, unit); // e.g. "in 5 days", "3 weeks ago"
const [value, unit] = dynamicDateDiff(someDate);const formatter = new Intl.NumberFormat("en-US", { style: "unit", unit, unitDisplay: "long" });const formatted = formatter.format(Math.abs(value)); // e.g. "5 days", "3 weeks" Copy
const [value, unit] = dynamicDateDiff(someDate);const formatter = new Intl.NumberFormat("en-US", { style: "unit", unit, unitDisplay: "long" });const formatted = formatter.format(Math.abs(value)); // e.g. "5 days", "3 weeks"
Calculates the difference between two dates, returning the value in the largest unit appropriate. For control over the unit, use dateDiff.
Returns values that can be passed to
Intl.RelativeTimeFormatorIntl.NumberFormat