Gilberto Quintero Gilberto Quintero - 1 year ago 240
jQuery Question

Handlebars date format issue

I have my handlerbars template with my property

{{#each claimsHistory}}

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.


Answer Source

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

{{#each claimsHistory}}
        {{formatTime lossDate "MM-DD-YYYY"}} 

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:

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download