How do I format Date/Time with jQuery Templates?

nfplee picture nfplee · Dec 3, 2010 · Viewed 16.9k times · Source

i've just started using jQuery Templates as my javascript template engine. My question is, how can i format a date (returned from a ASP.NET Json ActionResult) in the form:

/Date(1288709830000)/

I tried doing the following:

{{= $.format(new Date(parseInt(comment.DateCreated.substr(6))), 'd')}} 

Note the above uses the new jquery globalization plugin to add the $.format method. Also note that {{= comment.DateCreated }} is long hand for saying ${comment.DateCreated}.

I'd really appreciate it if you could help.

Answer

Kwex picture Kwex · Oct 7, 2011

This is what I used

var formatDate = function (datetime) {
    var dateObj = new Date(parseInt(datetime.replace("/Date(", "").replace(")/", ""), 10));
    return dateObj.format("dd-MMM-yyyy"); //01-Jun-2001
}

And this in my JQuery Template

${formatDate(InceptionDate)}