2013-05-03 78 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/