DateTime — GraphQL Custom Scalar

Author – ChilliCream

Date – 2024-12-24

1Overview

The DateTime scalar type represents a date and time with time zone offset information. It is intended for scenarios where representing a specific instant in time is required, such as recording when an event occurred, scheduling future events across time zones, or storing timestamps for auditing purposes.

Unlike local date-time scalars that represent calendar dates and wall-clock times without time zone context, DateTime represents a specific moment in time that can be unambiguously compared and ordered regardless of the observer’s time zone. This makes it suitable for applications where the absolute instant in time is important.

The scalar uses RFC 3339 format for serialization, specifically the date-time production from section 5.6, which includes both the date and time components along with a time zone offset.

3Result spec

A DateTime scalar must serialize to a string conforming to the RFC 3339 date-time production from section 5.6. This represents a date, time, and time zone offset in the format: YYYY-MM-DDTHH:mm:ss.fffffffff+HH:mm or YYYY-MM-DDTHH:mm:ss.fffffffffZ.

The format is:

The serialized value must include time zone offset information as specified in the RFC 3339 time-offset production.

3.1Examples

These are valid result values:

Value Explanation
"2023-12-24T15:30:00Z" A DateTime in UTC.
"2023-12-24t15:30:00z" The t and z may be lowercase.
"2023-12-24T15:30:00+00:00" A DateTime in UTC (explicit zero offset).
"2023-12-24T15:30:00-05:00" A DateTime with negative offset (EST).
"2023-12-24T15:30:00.123Z" A DateTime with millisecond precision.
"2023-12-24T15:30:00.123456789+01:00" A DateTime with nanosecond precision.

These are invalid result values:

Value Why is it invalid
"2023-12-24T15:30:00" Missing time zone offset.
"2023-12-24 15:30:00Z" Space instead of T or t separator.
"2023-12-24" Missing time component.
"15:30:00Z" Missing date component.
"2023-13-01T00:00:00Z" Invalid month (13).
"2023-12-32T00:00:00Z" Invalid day (32).
"2023-12-24T15:30:00.1234567890Z" More than 9 fractional second digits.
"2023-12-24T24:00:00Z" Invalid hour (24).
"2023-02-30T15:30:00Z" Invalid date (February 30th).
"2023-12-24T15:30:00+24:00" Invalid offset (exceeds ±23:59).

4Input spec

A DateTime scalar accepts string values conforming to the RFC 3339 date-time production from section 5.6, both as GraphQL literals and as JSON input values.

The input format matches the result format and must:

Implementations should validate:

4.1Examples

Valid input values:

GraphQL Literal:

mutation {
  scheduleEvent(startTime: "2023-12-24T15:30:00Z") {
    id
  }
}
mutation {
  scheduleEvent(startTime: "2023-12-24T15:30:00.123456789+01:00") {
    id
  }
}

JSON input:

{
  "startTime": "2023-12-24T15:30:00Z"
}
{
  "startTime": "2023-12-24T15:30:00-05:00"
}

Invalid input values:

Value Why is it invalid
"2023-12-24T15:30:00" Missing time zone offset.
"2023-12-24 15:30:00Z" Space instead of T or t separator.
"2023-12-24T25:00:00Z" Invalid hour (25).
"2023-12-24T15:60:00Z" Invalid minute (60).
"2023-02-30T15:30:00Z" Invalid date (February 30th).
"2023-12-24T15:30:00.1234567890Z" More than 9 fractional second digits.
"2023-12-24T15:30:00+25:00" Invalid offset (exceeds maximum).
"2023-12-24T15:30:00 UTC" Invalid offset format.

5References

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