0

您如何使用 打印日期underscore.js?我很惊讶显然有任何方法可以做到这一点不像ejs

这是我想做的

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

4

1 回答 1

0

Underscore 的模板(有意)简单且最小化,因此没有任何内置的格式化实用程序。但是,您可以将所需的任何 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/

于 2013-05-03T16:30:03.223 回答