Handlebars date format issue

天大地大妈咪最大 提交于 2020-12-05 12:27:55

问题


I have my handlerbars template with my property

{{#each claimsHistory}}
  <td>
    {{lossDate}} 
  </td>
{{/each}}

lossDate its my date time, it is rendered like this 2015-08-28T00:00:00

I want to display it as this 2015-08-28 without the time.

Thanks


回答1:


This is the perfect time to write a handlebars helper! Add this minor modification to your template:

{{#each claimsHistory}}
    <td>
        {{formatTime lossDate "MM-DD-YYYY"}} 
    </td>
{{/each}}

Then in a HandlebarsHelpers.js file that you include in your app:

Handlebars.registerHelper('formatTime', function (date, format) {
    var mmnt = moment(date);
    return mmnt.format(format);
});

moment is a popular JavaScript library for manipulating dates. The helper shown here receives your date and a format string as passed by your template, creates a moment date object, then formats it according to your specified format strings. For more info on moment, including how to get various formats, go here: http://momentjs.com/docs/.

Of course, you could use vanilla JS date manipulation instead of Moment.js, but how you implement the helper is up to you.




回答2:


You can use handlebars helpers. The following will help you achieve your goal.

  1. run the following on your terminal to add handlebars helpers to your config.json

    npm install --save handlebars-helpers
    
  2. Import the handlebar helper in your app.js using

    var helpers = require('handlebars-helpers')();
    
  3. Then in your handlebar template, you'll have

    {{#each claimsHistory}}
       <td>
          {{moment lossDate format="YYYY-MM-DD"}} 
       </td>
    {{/each}}
    


来源:https://stackoverflow.com/questions/32260117/handlebars-date-format-issue

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!