在Meteor Collection中插入moment.js对象 [英] Inserting a momentjs object in Meteor Collection
本文介绍了在Meteor Collection中插入moment.js对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个简单的Meteor集合,我正在尝试向其中插入一个具有momentjs属性的文档.因此,我这样做:
I have a simple Meteor collection and I am trying to insert a document that has a momentjs property into it. So I do:
docId = Col.insert({m: moment()});
但是,当我尝试使用
doc = Col.findOne({_id: docId})
我会像这样获得doc.m的无效日期":
I get "Invalid date" for doc.m like so:
Object {_id: "wnHzTpHHxMSyMxmu3", m: "Invalid date"}
有人吗?!
推荐答案
我强烈建议将日期存储为Date
对象,并在提取日期后使用moon对其进行格式化.例如:
I strongly recommend storing dates as Date
objects and using moment to format them after they are fetched. For example:
Posts.insert({message: 'hello', createdAt: new Date});
然后稍后要显示日期时:
Then later when you want to display the date:
var date = Posts.findOne().createdAt;
moment(date).format('MMMM DD, YYYY');
这篇关于在Meteor Collection中插入moment.js对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文