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 29, 2020 in Node-js by kartik
• 37,510 points
652 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
answered Nov 29, 2020 by Niroj
• 82,720 points

Related Questions In Node-js

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,720 points
2,173 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,720 points
2,471 views
0 votes
1 answer

How to use executables from a package installed locally in node_modules?

Hello @kartik, Use the npm bin command to get the ...READ MORE

answered Jul 13, 2020 in Node-js by Niroj
• 82,720 points
190 views
0 votes
1 answer

How can I set NODE_ENV=production on Windows?

Hello @kartik, It would be ideal if you ...READ MORE

answered Jul 13, 2020 in Node-js by Niroj
• 82,720 points
7,430 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,720 points
7,087 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,720 points
810 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,720 points
625 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,720 points
7,489 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,720 points
3,886 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,720 points
620 views