Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Spliting exact time from MongoDB createdAt field

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.

mongoDB screenshot

See the createdAt timestamp for a data created on 2023-08-17 22:30:02.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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?

>Solution :

There are some library exist that format you date and time and also make them separate.

  1. Dayjs : Link
  2. Moment : Link
  3. Luxon : Link
  4. date-fns : Link

But you can achieve same thing with JavaScript as well.

`${new Date('2023-08-17 22:30:02').getHours()}
 :${new Date('2023-08-17 22:30:02').getMinutes()}
 :${new Date('2023-08-17 22:30:02').getSeconds()}`
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading