Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/461.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/87.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在Meteor把手支架{{timestamp}中格式化日期_Javascript_Jquery_Node.js_Meteor_Handlebars.js - Fatal编程技术网

Javascript 在Meteor把手支架{{timestamp}中格式化日期

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') }

使用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')
});
然后在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,请注意,经过一些转换后,语法现在是