How can I format a date coming from MongoDB

0 votes

I'm using Jade to render my views from within Express.js. I am saving documents in MongoDB and using Mongoose to access my documents. I am saving a default date created when a new document is created and I am returning that date created attribute to the view, where is needs to be formatted. The format of the date being stored within MongoDB is:

sun nov 29 2020 20:14:56 GMT-0600 (CST)

How do I format this date in Jade (or Mongoose or Node.JS) coming back from MongoDB?

Nov 30, 2020 in Node-js by kartik
• 37,520 points
9,273 views

1 answer to this question.

0 votes

Hello @kartik,

JavaScript has built-in support for dates. First, to get your string into a Date object:

date =  new Date('sun nov 29 2020 20:14:56 GMT-0600 (CST)')

Now you can use various methods on the date to get the data you need:

date.toDateString() // "sun nov 29 2020 "
date.toUTCString()  // "sun nov 29 2020 20:14:56 GMT-0600 (CST)"
date.getMonth()     // 11
date.getDate()      // 29
date.getFullYear()  // 2020


Hope this helps!!

To know more about Mongodb, go for Mongodb training online without fail.

Thanks!

answered Nov 30, 2020 by Niroj
• 82,840 points

Related Questions In Node-js

0 votes
1 answer

Can I display a TV channel from TV tuner card using NodeJS for an Electron APP?

Tvheadend is a streaming server for live ...READ MORE

answered Jun 10, 2022 in Node-js by Neha
• 9,020 points
790 views
0 votes
1 answer

How do I manage MongoDB connections in a Node.js web application?

When the Node.js application starts, create a ...READ MORE

answered Jun 10, 2022 in Node-js by Neha
• 9,020 points
1,963 views
0 votes
1 answer

How can I pretty-print JSON using node.js?

Hello @kartik, JSON.stringify's third parameter defines white-space insertion ...READ MORE

answered Jul 12, 2020 in Node-js by Niroj
• 82,840 points
4,982 views
0 votes
1 answer

How can I update npm on Windows?

Hello @kartik, To update NPM, this worked for ...READ MORE

answered Jul 13, 2020 in Node-js by Niroj
• 82,840 points
6,455 views
+1 vote
1 answer

How to make anchor tag with routing using Laravel?

Hey @kartik, First you have to go to ...READ MORE

answered Mar 18, 2020 in Laravel by Niroj
• 82,840 points
23,021 views
0 votes
1 answer

What is redirection in Laravel?

Named route is used to give specific ...READ MORE

answered Mar 18, 2020 in Laravel by Niroj
• 82,840 points
3,000 views
0 votes
1 answer

How to install Laravel via composer?

Hello, This is simple you just need to ...READ MORE

answered Mar 23, 2020 in Laravel by Niroj
• 82,840 points
2,922 views
+1 vote
1 answer

What are named routes in Laravel and How can specify route names for controller actions?

Hey @kartik, Named routing is another amazing feature of ...READ MORE

answered Mar 23, 2020 in Laravel by Niroj
• 82,840 points
44,758 views
0 votes
1 answer

How can I get npm start at a different directory?

Hello @kartik, Try using: npm start --prefix path/to/your/app & inside ...READ MORE

answered Jul 14, 2020 in Node-js by Niroj
• 82,840 points
10,981 views
0 votes
1 answer

How do I “include” functions from my other files in nodejs?

Hello @kartik, You require any js file,so you just ...READ MORE

answered Jul 9, 2020 in Node-js by Niroj
• 82,840 points
3,430 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP