Helper Function with Variables


So I’m trying something crazy, I have JSON with a date per object like so:

"date": "1423206212417"

And here’s the crazy part, I’d love for it to be human readable so within template helper I have a function called dateFormat, and it’s barebones simple from a JavaScript perspective.

  dateFormat: function(date) {
    return new Date(date);

This yields INVALID DATE however, if I log console.log(date) inside the function I get 1423206212417 back as I would expect.

So if I run new Date(1423206212417) inside DevTools I get back Fri Feb 06 2015 01:03:32 GMT-0600 (CST) as expected, so any clues why the helpers return function isn’t passing this data back to my template? If I drop the new before Date then it just returns the Date as it is that very second, which isn’t helpful in my situation.

For fun I tried to create a helper that just returned whatever I passed in concatenated with a hardcoded string in front of it, this doesn’t seem to work either :confused:

I am very much a noob to Meteor and am helping my team evaluate frameworks for upcoming projects and I love a lot of benefits Meteor offers over the others we’re looking at, just can’t stand that I’m tripped up on something that seems so simple.


Probably because your JSON object is a string.

dateFormat: function(date) {
   return new Date(+date);

edit: the + forces JS to treat the variable as an integer.


Man, I had JUST realized that too, lol. I didn’t know about the + though I was about to reformat the JSON to not have strings for the date.

Thanks for the help @cstrat!


No worries!
You’re best to store it in JSON as an integer though if you can :smile: