Meteor Handlebars护腕中的格式日期{{timestamp}}

时间:2013-07-26 06:13:51

标签: javascript jquery node.js meteor handlebars.js

使用Meteor的Handlebar护腕时,如何将{{ timestamp }}的输出从Thu Jul 25 2013 19:33:19 GMT-0400 (Eastern Daylight Time)转换为Jul 25

尝试{{ timestamp.toString('yyyy-MM-dd') }},但却出错了

4 个答案:

答案 0 :(得分:40)

使用车把助手:

Template.registerHelper("prettifyDate", function(timestamp) {
    return new Date(timestamp).toString('yyyy-MM-dd')
});

然后在你的html:

{{prettifyDate timestamp}}

如果你使用片刻:

Template.registerHelper("prettifyDate", function(timestamp) {
    return moment(new Date(timestamp)).fromNow();
});

答案 1 :(得分:2)

这对我有用。

toString(“yyyy-MM-dd”) - 不转换它。

Template.registerHelper("prettifyDate", function(timestamp) {
    var curr_date = timestamp.getDate();
    var curr_month = timestamp.getMonth();
    curr_month++;
    var curr_year = timestamp.getFullYear();
    result = curr_date + ". " + curr_month + ". " + curr_year;
    return result;
});

答案 2 :(得分:1)

这对我有用

Handlebars.registerHelper("prettifyDate", function(timestamp) {
     return (new Date(timestamp)).format("yyyy-MM-dd");
});

答案 3 :(得分:0)

使用车把帮助器:

const exphbsConfig = exphbs.create({
    defaultLayout: 'main',
    extname: '.hbs',
    helpers:{

        prettifyDate:  function(timestamp) {
            function addZero(i) {
                if (i < 10) {
                  i = "0" + i;
                }
                return i;
            }

            var curr_date = timestamp.getDate();
            var curr_month = timestamp.getMonth();
            curr_month++;
            var curr_year = timestamp.getFullYear();

            var curr_hour = timestamp.getHours();
            var curr_minutes = timestamp.getMinutes();
            var curr_seconds = timestamp.getSeconds();

            result = addZero(curr_date)+ "/" + addZero(curr_month) + "/" + addZero(curr_year)+ '   ' +addZero(curr_hour)+':'+addZero(curr_minutes)+':'+addZero(curr_seconds);
            return result;
        }

    }    

});

app.engine('hbs', exphbsConfig.engine);
app.set('view engine', '.hbs');

然后在您的HTML中:

  <div class="card-footer">
      <small class="text-muted">Atualizada em: {{prettifyDate updatedAt}}    </small>
    </div>