Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I format Date/Time with jQuery Templates?

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.

like image 281
nfplee Avatar asked Dec 03 '10 00:12

nfplee


People also ask

What is the date format in jQuery?

format = All input formats valid for jQuery. format. date are valid for this method. The defaut format is MM/dd/yyyy HH:mm:ss.

What is Tmpl in jQuery?

jQuery.tmpl() is its official templating plugin. Developed by Microsoft, under MIT/GPL licence.

Is jQuery a template?

jQuery Template is applied to data objects or arrays and is rendered into the HTML using jQuery functions for DOM manipulation. In the following example, we will describe how JQuery template works. First, we must add reference to the JQuery library and JQuery Template.

What is the default date format in Javascript?

The string format should be: YYYY-MM-DDTHH:mm:ss. sssZ , where: YYYY-MM-DD – is the date: year-month-day. The character "T" is used as the delimiter.


1 Answers

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)}

like image 140
Kwex Avatar answered Oct 06 '22 09:10

Kwex