next-mdx icon indicating copy to clipboard operation
next-mdx copied to clipboard

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

Open tyteen4a03 opened this issue 4 years ago • 1 comments
trafficstars

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
---

tyteen4a03 avatar Apr 09 '21 23:04 tyteen4a03

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",
  })
}

shadcn avatar Apr 10 '21 06:04 shadcn