我正在尝试将Moment.js日期时间格式化为我的手柄模板的一部分。
实际样本:
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">
<div id="hb-content"></div>
<script id="hb-template" type="text/x-handlebars-template">
<small>Last restarted:</small>
<code>moment({{last_started}}).format('dddd, MMMM Do');</code>
<code>moment({{last_started}}).format('h:mma');</code>
</script>
<script type="text/javascript">
var temp = document.getElementById("hb-template").innerHTML;
var template = Handlebars.compile(temp);
var html = template({
last_started: "/Date(1463152740000)/"
})
document.getElementById('hb-content').innerHTML = html;
</script>
</asp:Content>
输出:
上次重启:时刻(/日期(1463152740000)/。)格式('dddd,MMMM Do'); 力矩(/日期(1463152740000)/)格式( 'H:MMA');
预期输出:
上次重启:5月13日星期五晚上9:15
答案 0 :(得分:2)
明白了:
<script id="hb-template" type="text/x-handlebars-template">
<small>Last restarted:</small>
<code>{{formatDate current_date 'dddd, MMMM Do'}}</code>
<code>{{formatDate current_date 'h:mm:ssa'}}</code>
</script>
<script type="text/javascript">
$(function () {
Handlebars.registerHelper("formatDate", function (datetime, format) {
return moment(datetime).format(format);
});
var temp = document.getElementById("hb-template").innerHTML;
var template = Handlebars.compile(temp);
var html = template({
last_started: "/Date(1463152740000)/",
current_date: new Date() //"/Date(1463152740000)/"
})
document.getElementById('hb-content').innerHTML = html;
});
</script>