How to hide the timestamp next to the date on a Sanity blog post
15 replies
Last updated: Apr 26, 2023
S
Hi everyone, please help. Does anyone know how to hide the timestamp next to date on sanity blog post? We prefer to just show the date.
Apr 26, 2023, 6:52 AM
A
You've to wrap the date inside toLocaleDateString()
Apr 26, 2023, 8:13 AM
S
Hi User, thanks for responding. Where can I see this code toLocaleDateString()?
Apr 26, 2023, 9:04 AM
R
{yourdate.toLocaleDateString()}. https://stackoverflow.com/questions/3552461/how-do-i-format-a-date-in-javascript
Apr 26, 2023, 9:11 AM
S
Hi robin, thanks for responding. Just want to ask this error just happened today after publishing a blog from sanity studio. Is this something I can modify on the schema?
Apr 26, 2023, 9:19 AM
A
Do your schema have a custom date field?
Apr 26, 2023, 11:09 AM
S
That's our schema for date field
Apr 26, 2023, 11:11 AM
R
You can do that on frontend. There are many libraries like " moment" to format datetime.. but if u wanna do that here in sanity schema, you can format date and time here { title: 'Launch Scheduled At',
name: 'launchAt',
type: 'datetime',
options: {
dateFormat: 'YYYY-MM-DD',
timeFormat: 'HH:mm',
timeStep: 15,
calendarTodayLabel: 'Today'
}
}, You can see thier docs for more, but you can do that on frontend just as easily
name: 'launchAt',
type: 'datetime',
options: {
dateFormat: 'YYYY-MM-DD',
timeFormat: 'HH:mm',
timeStep: 15,
calendarTodayLabel: 'Today'
}
}, You can see thier docs for more, but you can do that on frontend just as easily
Apr 26, 2023, 11:14 AM
A
By the way, Sanity itself adds a _createdAt and _updatedAt property when you create a document.
Apr 26, 2023, 11:18 AM
S
That's correct, as you can see on the previous blog post, it is showing correct format, however when we published today's blog, it is showing date and time stamp. And we are unable to share the link to our social media site as well because of the date showing 2023-04-25T08:57:00:000Z
Apr 26, 2023, 11:22 AM
S
I'm new to Sanity and Nextjs also, User & User. Appreciate your help.
Apr 26, 2023, 11:22 AM
S
I will try to modify the blog,js schema to below:title: 'Launch Scheduled At',
name: 'launchAt',
type: 'datetime',
options: {
dateFormat: 'YYYY-MM-DD',
timeFormat: 'HH:mm',
timeStep: 15,
calendarTodayLabel: 'Today'
}
}
name: 'launchAt',
type: 'datetime',
options: {
dateFormat: 'YYYY-MM-DD',
timeFormat: 'HH:mm',
timeStep: 15,
calendarTodayLabel: 'Today'
}
}
Apr 26, 2023, 11:23 AM
A
Do you really need a custom date field? Or you just want show to date when the post was created
Apr 26, 2023, 11:24 AM
S
Just the date (Ex. 2023-04-18) - as usually the studio function until today's issue
Apr 26, 2023, 11:25 AM
A
So you don't need a custom date field in your schema. Just add __createdAt inside your groq query. Afterwards, you can wrap it like new Date(__createdAt).toLocaleDateString() on the front end.
Apr 26, 2023, 11:27 AM
A
*[_type=="blogPosts"]{ _id, _createdAt, title, } like this
Apr 26, 2023, 11:29 AM
Sanity– build remarkable experiences at scale
Sanity is a modern headless CMS that treats content as data to power your digital business. Free to get started, and pay-as-you-go on all plans.