Skip to content
This repository was archived by the owner on Feb 27, 2022. It is now read-only.

Error with datetime in frontmatter: object` ("[object Date]") cannot be serialized as JSON #7

Open
tyteen4a03 opened this issue Apr 9, 2021 · 1 comment

Comments

@tyteen4a03
Copy link

I am getting this error:

Error: Error serializing `.post.frontMatter.lastUpdated` returned from `getStaticProps` in "/posts/[...slug]".
Reason: `object` ("[object Date]") cannot be serialized as JSON. Please only return JSON serializable data types.

With this post:

---
name: Post
lastUpdated: 2021-02-13
---
@shadcn
Copy link
Owner

shadcn commented Apr 10, 2021

Use a string:

---
name: Post
lastUpdated: "2021-02-13"
---

Then you can format it on render:

export function formatDate(input: string): string {
  const date = new Date(input)
  return date.toLocaleDateString("en-US", {
    month: "long",
    day: "numeric",
    year: "numeric",
  })
}

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants