Search code examples
meteorspacebars

How to alter the display of what Spacebars outputs?


So I have something like this in the template:

{{#each posts}}
    <li>{{date}}</li>
{{/each}}

This displays fine, but the problem is that my "date" variable comes out as Sat Feb 07 2015 19:47:13 GMT-0800 (PST), which is accurate but kind of long and unneccessary.

I want the date to appear simpler, like February 7, 2015 instead. However, please forgive me for my lack of understanding, but the only way in which I know how to do this is to store it in the pretty format to begin with, but this doesn't seem to be efficient if I have to do this every time I want something to look a little different.

Ideally, I'd like to wrap it in a moment() and produce something like this:

moment(date).format("MMMM Do YYYY")

I tried putting in this instead, but it doesn't work:

{{#each posts}}
    <li>{{moment(date).format("MMMM Do YYYY")}}</li>
{{/each}}

Solution

  • Add a helper

    Template.yourTemplate.helpers({
      formattedDate: function(){
        return moment(this.date).format("MM/DD/YYYY");  // or whatever format you prefer
      }
    });