Javascript 在Meteor把手支架{{timestamp}中格式化日期
使用Meteor的把手支架时,如何将Javascript 在Meteor把手支架{{timestamp}中格式化日期,javascript,jquery,node.js,meteor,handlebars.js,Javascript,Jquery,Node.js,Meteor,Handlebars.js,使用Meteor的把手支架时,如何将{{timestamp}}的输出从2013年7月25日周四19:33:19 GMT-0400(东部夏时制)转换为7月25日 已尝试{{timestamp.toString('yyyy-MM-dd')}但出现错误使用把手辅助工具: Template.registerHelper("prettifyDate", function(timestamp) { return new Date(timestamp).toString('yyyy-MM-dd') }
{{timestamp}}
的输出从2013年7月25日周四19:33:19 GMT-0400(东部夏时制)
转换为7月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();
});
这对我有用
Handlebars.registerHelper("prettifyDate", function(timestamp) {
return (new Date(timestamp)).format("yyyy-MM-dd");
});
这对我有用
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;
});
使用车把辅助工具:
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');
<div class="card-footer">
<small class="text-muted">Atualizada em: {{prettifyDate updatedAt}} </small>
</div>
const exphbsConfig=exphbs.create({
defaultLayout:'main',
extname:“.hbs”,
助手:{
prettifyDate:函数(时间戳){
函数addZero(i){
如果(i<10){
i=“0”+i;
}
返回i;
}
var curr_date=timestamp.getDate();
var curr_month=timestamp.getMonth();
当前月份++;
var curr_year=timestamp.getFullYear();
var curr_hour=timestamp.getHours();
var curr_minutes=timestamp.getMinutes();
var curr_seconds=timestamp.getSeconds();
结果=addZero(当前日期)+“/”+addZero(当前月份)+“/”+addZero(当前年份)+“+”+addZero(当前小时)+':“+addZero(当前分钟)+':”+addZero(当前秒);
返回结果;
}
}
});
附件发动机(“hbs”,附件发动机);
app.set('查看引擎','.hbs');
然后在html中:
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');
<div class="card-footer">
<small class="text-muted">Atualizada em: {{prettifyDate updatedAt}} </small>
</div>
Atualizada em:{{prettifyDate updatedAt}}
注意:标准会忽略传递给它的任何参数,并且ECMAScript不会定义任何其他可以基于字符串
格式化日期的方法,如'yyyy-MM-dd'
。如果您还没有包含一个修改toString()
的库,请查看建议。这里有一个更好的描述如何做->正如@JonathanLonowski上面提到的,值得注意的是timestamp.toString()
将忽略传递给它的任何参数。我发现使用MomentJS使所有这些都变得非常简单。@Akshat,请注意,经过一些转换后,语法现在是