Hex Core JS
    Preparing search index...

    Function dynamicDateDiff

    • 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

      Parameters

      • dateTime: DateTime

        The date time to calculate the relative time for.

      • relativeTo: DateTime = ...

        The date to compare to. Defaults to the current time.

      • options: DynamicDateDiffOptions = {}

        The options for the calculation.

      Returns [number, "year" | "month" | "day" | "hour" | "minute" | "second" | "week"]

      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"