TimeSpan — GraphQL Custom Scalar

Author – ChilliCream

Date – 2025-12-30

1Overview

The TimeSpan scalar type represents a duration of time. It is intended for scenarios where you need to represent time intervals, such as elapsed time, timeout durations, scheduling intervals, or any measurement of time that is not tied to a specific date or time.

Unlike date-time scalars which represent points in time, TimeSpan represents a length of time that could be applied to any point in time.

3Result spec

A TimeSpan scalar must serialize to a string representation conforming to the ISO 8601 duration format.

The format follows the pattern: P[n]Y[n]M[n]W[n]DT[n]H[n]M[n]S where:

Fractional seconds may be included after the seconds value, preceded by a decimal point.

3.1Examples

These are valid result values:

Value Explanation
"PT1H" 1 hour.
"PT30M" 30 minutes.
"P1DT2H30M" 1 day, 2 hours, and 30 minutes.
"P2W" 2 weeks.
"PT1H30M45S" 1 hour, 30 minutes, and 45 seconds.
"PT0.5S" 0.5 seconds (500 milliseconds).
"-PT15M" Negative 15 minutes.

These are invalid result values:

Value Why is it invalid
"1 hour" Not in ISO 8601 duration format.
"90" Missing duration designators.
"1:30:00" Not in ISO 8601 duration format.
123 Must be a string, not a number.

4Input spec

A TimeSpan scalar accepts string values representing durations in ISO 8601 duration format, both as GraphQL literals and as JSON input values.

Implementations should validate:

4.1Examples

Valid input values:

GraphQL Literal:

mutation {
  setSessionTimeout(duration: "PT30M") {
    id
  }
}

JSON input:

{
  "duration": "PT2H30M"
}
{
  "cacheExpiration": "P1DT12H"
}

Invalid input values:

Value Why is it invalid
"1 hour" Not in ISO 8601 duration format.
"90" Missing duration designators.
"01:00:00" Not in ISO 8601 duration format.
"" Empty string.

5References

  1. 1Overview
  2. 2Recommended name
  3. 3Result spec
    1. 3.1Examples
  4. 4Input spec
    1. 4.1Examples
  5. 5References