Skip to content

Commit

Permalink
📅 Fix representation of MyST date strings (#447)
Browse files Browse the repository at this point in the history
  • Loading branch information
agoose77 authored Aug 20, 2024
1 parent d4e240a commit d80bcce
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/new-bags-dance.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@myst-theme/frontmatter': patch
---

Don't apply timezone transforms to date strings
13 changes: 10 additions & 3 deletions packages/frontmatter/src/FrontmatterBlock.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
<time dateTime={date} className={classNames({ 'text-spacer': spacer })}>
{dateString}
Expand Down

0 comments on commit d80bcce

Please sign in to comment.