11

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

试过{{ timestamp.toString('yyyy-MM-dd') }}但它给出了一个错误

4

4 回答 4

41

使用车把助手:

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();
});
于 2013-07-26T06:29:45.247 回答
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;
});
于 2015-04-02T09:53:38.870 回答
1

这对我有用

Handlebars.registerHelper("prettifyDate", function(timestamp) {
     return (new Date(timestamp)).format("yyyy-MM-dd");
});
于 2013-10-25T15:11:58.793 回答
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>
于 2019-07-12T15:13:50.013 回答