javascript Meteor Handlebars 括号中的格式日期 {{ 时间戳}}
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17874181/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Format date in Meteor Handlebars bracers {{ timestamp }}
提问by Nyxynyx
When using Meteor's Handlebar bracers, how do you convert the output of {{ timestamp }}
from Thu Jul 25 2013 19:33:19 GMT-0400 (Eastern Daylight Time)
to Jul 25
?
使用 Meteor 的 Handlebar 护腕时,如何将{{ timestamp }}
from 的输出转换Thu Jul 25 2013 19:33:19 GMT-0400 (Eastern Daylight Time)
为Jul 25
?
Tried {{ timestamp.toString('yyyy-MM-dd') }}
but it gave an error
试过了,{{ timestamp.toString('yyyy-MM-dd') }}
但是报错
回答by Akshat
Use a handlebars helper:
使用把手助手:
Template.registerHelper("prettifyDate", function(timestamp) {
return new Date(timestamp).toString('yyyy-MM-dd')
});
Then in your html:
然后在你的 html 中:
{{prettifyDate timestamp}}
If you use moment:
如果您使用时刻:
Template.registerHelper("prettifyDate", function(timestamp) {
return moment(new Date(timestamp)).fromNow();
});
回答by Matej
This works for me.
这对我有用。
toString("yyyy-MM-dd") - doesn't convert it.
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;
});
回答by Harry
This worked for me
这对我有用
Handlebars.registerHelper("prettifyDate", function(timestamp) {
return (new Date(timestamp)).format("yyyy-MM-dd");
});
回答by Anderson Lima
Use a handlebars helper:
使用把手助手:
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');
Then in your html:
然后在你的 html 中:
<div class="card-footer">
<small class="text-muted">Atualizada em: {{prettifyDate updatedAt}} </small>
</div>