This is the documentation of version 1.0.0-rc.3. Go to the latest version, 1.0.0-rc.5.
TomTom Maps for JavaScript
    Preparing search index...

    Function formatDuration

    • Formats a duration in seconds into a human-readable time string.

      It shows hours and minutes, rounding and formatting by the duration's length.

      Parameters

      • seconds: number | undefined

        The duration in seconds. Values under 30 seconds return undefined.

      • Optionaloptions: TimeDisplayUnits

        Custom display units for the hours and minutes text.

      Returns string | undefined

      The formatted time, such as "1 hr 30 min" or "45 min", or undefined if the duration is too short.

      Rounding:

      • Durations under 30 seconds return undefined.
      • 30-59 seconds round to "1 min".
      • Minutes round to the nearest minute.
      • Hours show once the duration rounds to an hour or more.

      Formats:

      • Short duration: "15 min"
      • Long duration: "2 hr 30 min"
      • Exact hours: "3 hr 00 min"

      To override the default units, use options or the global TomTomConfig.

      // Basic usage
      formatDuration(0); // undefined (too short)
      formatDuration(20); // undefined (too short)
      formatDuration(30); // "1 min"
      formatDuration(60); // "1 min"
      formatDuration(100); // "2 min"
      formatDuration(1800); // "30 min"
      formatDuration(3599); // "1 hr 00 min"
      formatDuration(3660); // "1 hr 01 min"
      formatDuration(7200); // "2 hr 00 min"
      formatDuration(36120); // "10 hr 02 min"

      // Custom units
      formatDuration(3660, { hours: 'h', minutes: 'm' });
      // Returns: "1 h 01 m"

      // Route travel time
      const route = await calculateRoute({ ... });
      const travelTime = formatDuration(route.features[0].properties.summary.travelTimeInSeconds);
      console.log(`Estimated time: ${travelTime}`);
      // Output: "Estimated time: 2 hr 15 min"