I have a MongoDB
collection which contains some data created by users. I want to get exact time from createdAt
filed. Please see the below image.
See the createdAt timestamp for a data created on 2023-08-17 22:30:02
.
I’m building a next.js
app. I’m able to split date from the timestamp using slice()
method. But time given here is in some another format. Can anyone help me to get the time in standard format?
2
Answers
There are some library exist that format you date and time and also make them separate.
But you can achieve same thing with JavaScript as well.
I would suggest you handle the date formatting at server side to avoid potential inconsistent parsing of timezone between client side and server side using
$dateToString
.Mongo Playground