如何在Node.js中格式化来自Mongoose的日期? [英] How do I format dates from Mongoose in Node.js?

查看:445
本文介绍了如何在Node.js中格式化来自Mongoose的日期?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试更改从Mongo数据库获取的日期格式.目前,它们看起来像这样:

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)

我尝试对它们调用.toString('yyyy-MM-dd'),但没有任何变化.我不知道它们是Date对象还是原始字符串.

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.

我尝试检查过Mongoose手册并搜索了一大堆,但还没有发现任何东西.

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

有什么想法吗?

推荐答案

您必须首先创建一个Date对象:

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;
// ...

这篇关于如何在Node.js中格式化来自Mongoose的日期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆