Skip to content
KeystoneJS LogoKeystoneJS

DateTime

Stores a point in time and a time zone offset.

Usage

JS
const { DateTime } = require('@keystonejs/fields');

keystone.createList('User', {
  fields: {
    lastOnline: {
      type: DateTime,
      format: 'dd/MM/yyyy HH:mm O',
      yearRangeFrom: 1901,
      yearRangeTo: 2018,
      yearPickerType: 'auto',
    },
  },
});

Config

OptionTypeDefaultDescription
formatString--Defines the format of the string that the component generates
yearRangeFromStringThe current year - 100Defines the starting point of the year range, e.g. 1918
yearRangeToStringThe current yearDefines the ending point of the range in the yearSelect field , e.g. 2018
yearPickerTypeStringautoDefines the input type for the year selector
isRequiredBooleanfalseDoes this field require a value?
isUniqueBooleanfalseAdds a unique index that allows only unique values to be stored

format

Defines the format of the string using unicode tokens. For example, dd/MM/yyyy HH:mm O.

Documentation of all the available tokens on Unicode website

yearRangeFrom

The DateTime component includes an input that allows the user to change the current year from a range of options. This prop allows the user to set the beginning of that range.

The default value for this field is 100 years before the current year.

yearRangeTo

The DateTime component includes an input that allows the user to change the current year from a range of options. This prop allows the user to set the end of that range.

The default value for this field is the current year.

yearPickerType

The DateTime component includes an input that allows the user to change the current year from a range of options. This prop allows the user to change the type of that input.

OptionDescription
inputGenerates an input that allows the user to type in a value
selectGenerates a dropdown menu that allows the user to select a value from a list
autoWill generate a select if the range is 50 or less, otherwise will generate an input

GraphQL

The DateTime field type adds a custom scalar DateTime and uses it for input and output fields.

Storage

Mongoose adapter

On the Mongoose adapter the DateTime value is stored across three fields:

Field nameSchema typeDescription
${path}StringThe full timestamp with offset as a ISO8601 string
${path}_utcDateThe timestamp as a native JS-style epoch
${path}_offsetStringThe offset component as string

The isRequired config option is enforces by KeystoneJS only.

Knex adapter

On the Knex adapter the DateTime value is stored across two fields:

Column nameKnex typeDescription
${path}_utctimestampThe timestamp in UTC
${path}_offsettextThe offset component as string

The isRequired config option is enforced by KeystoneJS. If the value is equal to true, the column is set as not nullable.

On this page

  • Usage
  • Config
  • GraphQL
  • Storage
  • Mongoose adapter
  • Knex adapter
Edit on GitHub