Skip to content

Update datetime field to correctly store in UTC #1401

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion docs/src/content/pages/fields/datetime.mdoc
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@ summary: The datetime field stores a Datetime string.
---
{% field-demo field="datetime" /%}

The `datetime` field stores a Datetime string, collected from an `<input type="datetime-local" />` form field.
The `datetime` field stores an [ISO 8601](https://en.wikipedia.org/wiki/ISO_8601) formatted datetime string in UTC time (i.e. YYYY-MM-DDTHH:MM:SS.000Z), collected from an `<input type="datetime-local" />` form field.

{% aside icon="☝️" %}
Datetimes are stored in UTC time, but displayed/edited in the user's local timezone.
{% /aside %}


## Usage example

Expand Down
16 changes: 5 additions & 11 deletions packages/keystatic/src/form/fields/datetime/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,10 @@ export function datetime<IsRequired extends boolean | undefined>({
return null;
}
if (typeof defaultValue === 'string') {
return defaultValue;
return new Date(defaultValue).toISOString();
}
if (defaultValue.kind === 'now') {
const now = new Date();
return new Date(now.getTime() - now.getTimezoneOffset() * 60 * 1000)
.toISOString()
.slice(0, -8);
return new Date().toISOString();
}
return null;
},
Expand All @@ -54,19 +51,16 @@ export function datetime<IsRequired extends boolean | undefined>({
return null;
}
if (value instanceof Date) {
return value.toISOString().slice(0, -8);
return value.toISOString();
}
if (typeof value !== 'string') {
throw new FieldDataError('Must be a string or date');
}
return value;
return new Date(value).toISOString();
},
serialize(value) {
if (value === null) return { value: undefined };
const date = new Date(value + 'Z');
date.toJSON = () => date.toISOString().slice(0, -8);
date.toString = () => date.toISOString().slice(0, -8);
return { value: date };
return { value: new Date(value).toISOString() };
},
validate(value) {
const message = validateDatetime(validation, value, label);
Expand Down
10 changes: 9 additions & 1 deletion packages/keystatic/src/form/fields/datetime/ui.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,14 @@ import { useReducer } from 'react';
import { validateDatetime } from './validateDatetime';
import { FormFieldInputProps } from '../../api';

function convertUTCToLocal(datetime: string | null): string {
if (!datetime) return '';
const date = new Date(datetime);
const offset = date.getTimezoneOffset();
const localDate = new Date(date.getTime() - offset * 60 * 1000);
return localDate.toISOString().slice(0, 16);
}

export function DatetimeFieldInput(
props: FormFieldInputProps<string | null> & {
label: string;
Expand All @@ -23,7 +31,7 @@ export function DatetimeFieldInput(
props.onChange(val === '' ? null : val);
}}
autoFocus={props.autoFocus}
value={props.value === null ? '' : props.value}
value={convertUTCToLocal(props.value)}
onBlur={onBlur}
isRequired={props.validation?.isRequired}
errorMessage={
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ export function validateDatetime(
value: string | null,
label: string
) {
if (value !== null && !/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}$/.test(value)) {
if (value !== null && isNaN(Date.parse(value))) {
return `${label} is not a valid datetime`;
}

Expand All @@ -12,15 +12,16 @@ export function validateDatetime(
}
if ((validation?.min || validation?.max) && value !== null) {
const datetime = new Date(value);
const utcDatetime = new Date(datetime.getTime() - datetime.getTimezoneOffset() * 60000);
if (validation?.min !== undefined) {
const min = new Date(validation.min);
if (datetime < min) {
if (utcDatetime < min) {
return `${label} must be after ${min.toISOString()}`;
}
}
if (validation?.max !== undefined) {
const max = new Date(validation.max);
if (datetime > max) {
if (utcDatetime > max) {
return `${label} must be no later than ${max.toISOString()}`;
}
}
Expand Down