keystatic icon indicating copy to clipboard operation
keystatic copied to clipboard

`datetime` field: incorrect UTC conversion behavior

Open mackeyguenther opened this issue 11 months ago • 0 comments

The datetime field currently captures dates in local time, using an <input> of type datetime-local. However, these values are serialized to content files as UTC by appending a "Z", without correctly adjusting the local time to UTC:

https://github.com/Thinkmill/keystatic/blob/4c4b0efa8ecfc45053dee992f7ceb8566c520ede/packages/keystatic/src/form/fields/datetime/index.tsx#L64C1-L70C7

   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 };
    },

This implementation requires content users to manually adjust deserialized Date objects to the time zone of the content editor, which may not always be known.

Instead, the datetime fields should convert its data from local time to UTC before serialization. This would ensure predictable alignment between content editors and content users.

Overview:

  • The datetime-local input type captures dates in local time.
  • The current implementation appends a "Z" to the local time, indicating UTC, without converting the local time to UTC.
  • This can lead to inconsistencies and unexpected behavior when the serialized date is interpreted as UTC.
  • The datetime component should convert the local time to UTC before serializing.
  • This change will ensure that the serialized date is consistently interpreted as UTC, leading to more predictable behavior.

mackeyguenther avatar Feb 08 '25 04:02 mackeyguenther