我正在使用 Jade 从 Express.js 中呈现我的 View 。我在 MongoDB 中保存文档并使用 Mongoose 访问我的文档。我正在保存创建新文档时创建的默认日期,并将该日期创建属性返回到需要格式化的 View 。存储在 MongoDB 中的日期格式为:
Thu Dec 29 2011 20:14:56 GMT-0600 (CST)
我的问题是:如何在从 MongoDB 返回的 Jade(或 Mongoose 或 Node.JS)中格式化这个日期?
最佳答案
我正好有这个要求(expressjs、mongoose、jade),这就是我自己解决的方法。
首先我安装了momentjs使用 npm install moment
。
然后我用这个来查看:
var moment = require('moment');
app.get('/', function(req, res){
// find all jobs using mongoose model
Job.find().exec(function(err, items){
// pass moment as a variable
res.render('status', { 'jobs': items, moment: moment });
})
});
然后在 Jade 中这样使用它:
table
tr
td Subject
td Posted at
td Status
each job, i in jobs
tr
td #{job.subject}
td #{moment(job.postedAt).format("YYYY-MM-DD HH:mm")}
td #{job.status}
关于node.js - 如何格式化来自 MongoDB 的日期?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8675642/