fix: timezone issue with ISO dates

If the date is in YYYY-MM-DD format, which is the default for the Obsidian 'date' file property, it
will be interpreted as UTC midnight which will mean it renders incorrectly as the previous day when
passed to 'getLocaleDateString'. Adding a time makes it interpret as midnight in the local timezone
instead so it will render as expected.
This commit is contained in:
Linus Rachlis 2024-11-24 10:56:10 -05:00
parent e1d754ef79
commit c78f77b592

View File

@ -48,7 +48,15 @@ export const CreatedModifiedDate: QuartzTransformerPlugin<Partial<Options>> = (u
created ||= st.birthtimeMs created ||= st.birthtimeMs
modified ||= st.mtimeMs modified ||= st.mtimeMs
} else if (source === "frontmatter" && file.data.frontmatter) { } else if (source === "frontmatter" && file.data.frontmatter) {
created ||= file.data.frontmatter.date as MaybeDate if (!created) {
created = file.data.frontmatter.date as MaybeDate
if (typeof created === "string" && /^\d{4}-\d{2}-\d{2}$/.test(created)) {
// If the date is in YYYY-MM-DD format, it will be interpreted as UTC midnight
// which will mean it renders incorrectly as the previous day. Adding a time
// makes it interpret as the local timezone instead so it will render correctly.
created += " 00:00"
}
}
modified ||= file.data.frontmatter.lastmod as MaybeDate modified ||= file.data.frontmatter.lastmod as MaybeDate
modified ||= file.data.frontmatter.updated as MaybeDate modified ||= file.data.frontmatter.updated as MaybeDate
modified ||= file.data.frontmatter["last-modified"] as MaybeDate modified ||= file.data.frontmatter["last-modified"] as MaybeDate