Underscore templates let you call function and output text in any way you see fit via print
. For example, to convert your timestamp to a date, you could use something like this
<script type="text/template" id="tpl-1">
<span class="label label-info"><% print(new Date(date*1000)) %></span>
</script>
Note that I assume the timestamp comes from PHP, thus in seconds. In Javascript, the timestamps are expected to be in milliseconds, that's why I multiply it by 1000.
If your timestamps come from Javascript, use
<script type="text/template" id="tpl-1">
<span class="label label-info"><% print(new Date(date)) %></span>
</script>
Formatting this date object could be done like this
<script type="text/template" id="tpl-2">
<span class="label label-info"><%
var d = new Date(date*1000), // or d = new Date(date)
fragments = [
d.getDate(),
d.getMonth() + 1,
d.getFullYear()
];
print(fragments.join('/'));
%></span>
</script>
Or factorize all this into a function call (here on _.template
but you could store it anywhere)
_.template.formatdate = function (stamp) {
var d = new Date(stamp*1000), // or d = new Date(date)
fragments = [
d.getDate(),
d.getMonth() + 1,
d.getFullYear()
];
return fragments.join('/');
};
<script type="text/template" id="tpl-3">
<span class="label label-info"><%= _.template.formatdate(date) %></span>
</script>
And a demo http://jsfiddle.net/Dyzm8/