How to print date with underscore.js -


how print dates using underscore.js? surprised there apparently way unlike ejs

here do

<%= message.created_at.getfullyear() %>-<%= message.created_at.getmonth() + 1 %>-<%= message.created_at.getdate() %>

underscore's templates (intentionally) simple , minimal there aren't built in formatting utilities. however, can put whatever javascript expressions want inside <%= ... %> can add own formatting utilities. in javascript:

window.fmt = {     iso_date: function(d) {         // favorite iso 8601 date formatter goes here,         // quick hack (which won't work in older ies)         // demonstration purposes.         return d.toisostring().replace(/t.*$/, '');     },     // other formatting functions need go here... }; 

and call fmt.iso_date in template thusly:

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

demo: http://jsfiddle.net/ambiguous/4ufs4/


Comments

Popular posts from this blog

php - Why I am getting the Error "Commands out of sync; you can't run this command now" -

linux - Does gcc have any options to add version info in ELF binary file? -

java - Are there any classes that implement javax.persistence.Parameter<T>? -