Module 03
Page 05
2 min

Datetime

Creating date and time fields with the Datetime schema type


With the Datetime schema type, we can store both a date and time in the ISO-8601 format in UTC (Coordinated Universal Time.) The format of how it's saved doesn't really matter to us for now - that's a later thing to talk about.

Let's make a field for Posted At, the "day and time the blog post was intended to be posted."

Like Number, we're adding this field for the sake of learning it - having the _createdAt and _updatedAt fields in documents by default basically eliminates the need for having this in a blog post.
/schemas/blogPost.js
...,{  name: 'postedAt',  type: 'datetime',  title: 'Posted At',  group: 'metadata',},...

And here's how it looks in Studio:

You can see you not only can type in a date and time, but also select it from a popover with more options. Set it to whatever date and time you'd like.

Date/Time Formats and Time Step

We have a few options when it comes to Datetime - we can set a date and time format that differs from the default. You can use any Moment format option.

{  name: 'postedAt',  type: 'datetime',  title: 'Posted At',  group: 'metadata',  options: {    dateFormat: 'YYYY-MM-DD',    timeFormat: 'HH:mm',    timeStep: 10,  },},

There's a few things going on here:

  • dateFormat: Currently, we have it set to the default - there isn't any current reason for us to change it. This is purely a visual change that influences what your field shows - you can click on the Calendar Button to see specifics.
  • timeFormat: This is also set to the default, and like Date Format, is purely a visual change.
  • timeStep: Time Step is the incrementing value we want for our minutes when choosing the time. Currently, 10 will let us choose 00, 10, 20, 30 etc.

We can apply the same validation required rule to this Datetime schema, as well as a minimum and maximum date, if you need that.

Ritesh Kanchi © 2025