2013-05-03 81 views
0

如何使用underscore.js打印日期?我很惊讶,显然有什么办法做到这一点不像ejs如何使用underscore.js打印日期

这是我想要做的

<%= Message.created_at.getFullYear() %>-<%= Message.created_at.getMonth() + 1 %>-<%= Message.created_at.getDate() %>

回答

0

下划线的模板(故意)简单的和最小的所以没有任何东西内置格式化工具。但是,您可以在<%= ... %>之内放入任何想要的JavaScript表达式,以便您可以轻松添加自己的格式化实用程序。你可以做这样的事情在你的JavaScript:

window.fmt = { 
    iso_date: function(d) { 
     // Your favorite ISO 8601 date formatter goes here, this 
     // is just a quick hack (which won't work in older IEs) 
     // for demonstration purposes. 
     return d.toISOString().replace(/T.*$/, ''); 
    }, 
    // Any other formatting functions you need go here... 
}; 

,然后调用fmt.iso_date在你的模板正是如此:

<%= fmt.iso_date(Message.created_at) %> 

演示:http://jsfiddle.net/ambiguous/4Ufs4/