非常基本,我正在尝试在网站顶部显示当前时间(以小时、分钟和秒为单位)。
我刚刚接触 Meteor,还没有找到显示时间的方法。我尝试过 MomentJS、Chronos 和其他一些不同的软件包,并尝试过使用普通 JS 的旧式简单方法来尝试显示它,但它从未显示。
我已经掌握了模板和 IronRouter 的窍门,但就是无法完全显示时间。我不确定这是因为 JS 在 Meteor 中的渲染方式不同还是我只是做错了。
对于 Chronos,我尝试过:
Template.foo.helpers({
currentTime : function() {
return Chronos.date(); // updates every second
}
});
以此为模板:
<template name="foo">
current time: {{currentTime}}
</template>
但是没有骰子。有人能指出我正确的方向吗?谢谢!
最佳答案
这就是我在 UTC 时间上的做法...
我定义了一个全局模板助手(因为我需要在多个模板中显示时间)。
Template.registerHelper('currentUtcTime', function() {
return Chronos.moment().utc().format('MMM DD HH:mm:ss');
});
然后在模板中我使用这样的助手。
<h6 class="center-align">Current Time: {{currentUtcTime}} (UTC)</h6>
我使用了 Chronos.moment()
因为这使我可以使用格式字符串轻松显示时间。
如果您想要本地时间,那么您的助手将如下所示。
Template.registerHelper('currentLocalTime', function() {
return Chronos.moment().format('MMM DD HH:mm:ss');
});
关于javascript - 在 Meteor 中显示当前时间?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43078627/