SDCore
SDCore

Reputation: 41

Display Current Time in Meteor?

Very basic, I'm trying to display the current time (in hours, minutes, and seconds) at the top of my website.

I'm just getting into Meteor and have yet to find a way to display the time. I've tried MomentJS, Chronos, and a few other different packages, and tried the old plain way of using normal JS to try and display it, but it never displays.

I've got the hang of templates and IronRouter, but just haven't been able to fully display time. I'm not sure if it's because JS is rendered differently in Meteor or if I'm just doing it wrong.

With Chronos, I've tried this:

Template.foo.helpers({
   currentTime : function() {
       return Chronos.date(); // updates every second
   }
});

with this as the template:

<template name="foo">
   current time: {{currentTime}}
</template>

But no dice. Could anyone point me in the right direction? Thanks!

Upvotes: 0

Views: 494

Answers (1)

jordanwillis
jordanwillis

Reputation: 10705

This is how I do it for UTC time...

I defined a global template helper (because I need to display the time in several templates).

Template.registerHelper('currentUtcTime', function() {
  return Chronos.moment().utc().format('MMM DD HH:mm:ss');
});

Then from within a template I use the helper like this.

<h6 class="center-align">Current Time: {{currentUtcTime}} (UTC)</h6>

I used Chronos.moment() because that allows me to easily display the time however I want using format strings.

If you want local time then your helper would look like this.

Template.registerHelper('currentLocalTime', function() {
  return Chronos.moment().format('MMM DD HH:mm:ss');
});

Upvotes: 1

Related Questions