diff --git a/.changeset/new-bags-dance.md b/.changeset/new-bags-dance.md new file mode 100644 index 00000000..cc977d1f --- /dev/null +++ b/.changeset/new-bags-dance.md @@ -0,0 +1,5 @@ +--- +'@myst-theme/frontmatter': patch +--- + +Don't apply timezone transforms to date strings diff --git a/packages/frontmatter/src/FrontmatterBlock.tsx b/packages/frontmatter/src/FrontmatterBlock.tsx index ef3fae50..56dab11c 100644 --- a/packages/frontmatter/src/FrontmatterBlock.tsx +++ b/packages/frontmatter/src/FrontmatterBlock.tsx @@ -74,9 +74,16 @@ export function DateString({ spacer?: boolean; }) { if (!date) return null; - const d = new Date(date); // This is in the users timezone - const utcDate = new Date(d.getUTCFullYear(), d.getUTCMonth(), d.getUTCDate()); - const dateString = utcDate.toLocaleDateString('en-US', format); + // Parse the date + // As this is a YYYY-MM-DD form, the parser interprets this as a UTC date + // (see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date#the_epoch_timestamps_and_invalid_date) + const utcDate = new Date(date); + + // Now cast our UTC-date into the local timezone + const localDate = new Date(utcDate.getUTCFullYear(), utcDate.getUTCMonth(), utcDate.getUTCDate()); + + // Then format as human-readable in the local timezone. + const dateString = localDate.toLocaleDateString('en-US', format); return (