使用 Meteor 的 Handlebar 护腕时,如何将{{ timestamp }}
from 的输出转换Thu Jul 25 2013 19:33:19 GMT-0400 (Eastern Daylight Time)
为Jul 25
?
试过{{ timestamp.toString('yyyy-MM-dd') }}
但它给出了一个错误
使用 Meteor 的 Handlebar 护腕时,如何将{{ timestamp }}
from 的输出转换Thu Jul 25 2013 19:33:19 GMT-0400 (Eastern Daylight Time)
为Jul 25
?
试过{{ timestamp.toString('yyyy-MM-dd') }}
但它给出了一个错误
使用车把助手:
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();
});
这对我有用。
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;
});
这对我有用
Handlebars.registerHelper("prettifyDate", function(timestamp) {
return (new Date(timestamp)).format("yyyy-MM-dd");
});
使用车把助手:
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>