如何在流星模板中格式化日期 [英] How to format date in meteor template
问题描述
我需要以格式mm-dd-yyyy显示数据库中的日期。由于它以ISO格式保存在 mongodb
中,我怎样才能在模板中转换它?
这里是我的代码。
Template.templatename.vname = function(){
返回文章。找();
}
并且在模板中
{{#each vname}}
pre>
{{date}}
{{/ each}}
现在它显示为
Tue Feb 04 2014 00:00:00 GMT + 0530(IST)
我需要将它显示为
mm-dd-yyyy
解决方案您可能想要创建一个全局帮助器,如:
<$ (日期).format('MM-DD-YYYY');
});
然后你可以像这样使用它:
{{#each vname}}
{{formatDate date}}
{{/ each}}
pre>
该解决方案取决于时刻是一个方便的日期操纵库。如果你更喜欢不使用时刻来产生字符串,这里有很多答案,包括这一个。
I need to display a date from database in the format 'mm-dd-yyyy'. As its saved in ISO format in
mongodb
how can I convert it in the template ? Here is my code.Template.templatename.vname = function () { return Posts.find(); }
And in template
{{#each vname}} {{ date }} {{/each}}
Now its getting displayed like
Tue Feb 04 2014 00:00:00 GMT+0530 (IST)
I need to show it as
mm-dd-yyyy
解决方案You may want to create a global helper like:
Template.registerHelper('formatDate', function(date) { return moment(date).format('MM-DD-YYYY'); });
Then you can use it like:
{{#each vname}} {{formatDate date}} {{/each}}
This solution depends on moment which is a handy date manipulation library. If you prefer to produce the string without using moment, there are a number of answers for this including this one.
这篇关于如何在流星模板中格式化日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!