在我的应用程序中,我有一个名为Messages的集合。定义如下:
var email = userEmail();
var time = new Date().getTime();
//value is the text of the message
message = {user: Meteor.userId(), time: time, message: value};
Messages.insert(message);
在我的模板中,我有以下内容:
{{#each messages}}
<li class="panel panel-default">
<p class="date">{{time}}</p>
<a href="http://{{message}}">{{message}}</a>
</li>
{{/each}}
当然,当我访问我的网页时,它会将时间显示为一个纪元时间戳,例如:1398954569368
我想对其进行处理,{{time}}
以便以人类可读的格式显示它。
如何或在哪里可以访问{{time}}
变量,以便可以对其执行JavaScript操作?
此数字是Date()。getTime()返回的值。在日期对象上签出MDN:
Date.prototype.getTime()返回指定日期的数字值,该值是自1970年1月1日UTC 00:00:00(以前的时间为负)以来的毫秒数。
假设您的信息可以通过该路径访问,则可以使用模板帮助器中的Moment.js来访问/修改日期/时间对象。举个例子:
Template.yourTemplateName.time = function() {
return moment(yourDateObject).fromNow();
};
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句