Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

How do I format dates from Mongoose in Node.js?

How do I format dates from Mongoose in Node.js?

Problem

I'm trying to change the Format of the dates I'm getting from my Mongo database. Currently they look like this:

Fri Sep 16 2011 19:05:17 GMT+0900 (JST)

I've tried calling .toString('yyyy-MM-dd') on them but nothing changes. I don't know if they're Date objects or just raw strings.

I've tried checking the Mongoose manual and googling a bunch, but not found anything yet.

Any ideas?

Problem courtesy of: Ben Humphreys

Solution

you have to create a Date object first:

var date = new Date(dateStr);  // dateStr you get from mongodb

var d = date.getDate();
var m = date.getMonth()+1;
// ...
Solution courtesy of: Philipp Kyeck

Discussion

View additional discussion.



This post first appeared on Node.js Recipes, please read the originial post: here

Share the post

How do I format dates from Mongoose in Node.js?

×

Subscribe to Node.js Recipes

Get updates delivered right to your inbox!

Thank you for your subscription

×