Glutch
April 9, 2015, 6:33pm
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
rafael
April 9, 2015, 6:39pm
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}}
Glutch
April 9, 2015, 6:46pm
3
That seems really confusing and i couldnt get it to work
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”}}?
rafael
April 9, 2015, 6:49pm
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}}
Glutch
April 9, 2015, 6:52pm
5
rafael:
MomentJS
Oh. Your code works now when i installed momentjs ^^
meteor add momentjs:moment
Thanks!
Glutch
April 9, 2015, 7:09pm
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/
nkrisc
April 9, 2015, 7:21pm
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