车把日期格式问题 [英] Handlebars date format issue

查看:84
本文介绍了车把日期格式问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有带有属性的处理程序模板

I have my handlerbars template with my property

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

lossDate它是我的约会时间,它的呈现方式如下2015-08-28T00:00:00

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

我想在没有时间的情况下将其显示为2015-08-28.

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

谢谢

推荐答案

这是编写车把助手的最佳时机!将此较小的修改添加到您的模板中:

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}}

然后在您包含在应用程序中的HandlebarsHelpers.js文件中:

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是用于处理日期的流行JavaScript库.此处显示的帮助程序将接收您的日期和模板传递的格式字符串,创建时刻日期对象,然后根据您指定的格式字符串对其进行格式设置.有关此刻的更多信息,包括如何获取各种格式,请访问此处: http://momentjs.com/docs/.

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/.

当然,您可以使用普通JS日期操作来代替Moment.js,但是如何实现帮助程序由您决定.

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

这篇关于车把日期格式问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆