Filters in Meteor


#1

Hey!

I’m wondering about filters in Meteor.
In Angular you can use {{mytime | date}} to display a nice shorter date. And also for example {{money | currency}} to re-format your integer to $200.00 instead of 200.

So my question is, how do you do this in Meteor?

I’ve got

var time = new Date();

And i want to display it nicely. Perhaps just “20:33”

Thanks


#2

Use a helper.

E.g.: using moment to format a date:

Template.yourTemplate.helpers({
  'formatDate': function(date) {
    return moment.utc(date).format("DD/MM/YYYY");
  },
});

When displaying, just add the helper name first:

{formatDate myDate}}

#3

That seems really confusing and i couldnt get it to work :frowning:

I’m currently displaying {{timeCreated}} in my html, in a template. Which returns “Thu Apr 09 2015 20:24:03 GMT+0200 (CEST)”

Is there no way like angular? {{timeCreated | date:“MM/DDD/YYYY”}}?


#4

Just like I said, define a helper to format your date as you prefer. I recommend MomentJS for that.

So, in your html, just add the helper name before your variable. Supposing your helper is named “formatDate”, it’d be:

{{formatDate timeCreated}}

#5

Oh. Your code works now when i installed momentjs ^^

meteor add momentjs:moment

Thanks!


#6

Do you know how i can change it from “09:08:30 PM” to “21:08:30”? Cant find it on http://momentjs.com/


#7

http://momentjs.com/docs/#hour-minute-second-millisecond-and-offset-tokens

Looks like if you use upper case hour tokens in your format HH it will use 24 hour time. Lower case hh is 12 hours time used with a for AM/PM: hh a


#8

Awesome, thanks a lot!