Skip to content
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

datetime field: incorrect UTC conversion behavior #1400

Open
mackeyguenther opened this issue Feb 8, 2025 · 0 comments · May be fixed by #1401
Open

datetime field: incorrect UTC conversion behavior #1400

mackeyguenther opened this issue Feb 8, 2025 · 0 comments · May be fixed by #1401

Comments

@mackeyguenther
Copy link

mackeyguenther commented Feb 8, 2025

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 mackeyguenther changed the title datetime field: incorrect UTC converstoin datetime field: incorrect UTC conversion Feb 8, 2025
@mackeyguenther mackeyguenther changed the title datetime field: incorrect UTC conversion datetime field: incorrect UTC conversion behavior Feb 8, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant