Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Handlebars date format issue

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

like image 353
Gilberto Quintero Avatar asked Aug 27 '15 21:08

Gilberto Quintero


People also ask

How to change date format in handlebars?

Solution. There are two ways of formatting the value: Create a Handlebars helper {{format-date}} and use it in your template.

Is handlebars a template language?

Handlebars is a simple templating language. It uses a template and an input object to generate HTML or other text formats. Handlebars templates look like regular text with embedded Handlebars expressions.

Is handlebars a template engine?

Handlebars: What it is and Why to Use itHandlebars is a logic-less templating engine that dynamically generates your HTML page.


1 Answers

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.

like image 152
stolli Avatar answered Oct 04 '22 23:10

stolli