3

I have the following JSON date returned from a MVC page e.g.

"DateProcessed":"\/Date(1258125238090)\/"

and I am using JTemplates to process the data as below.

$('#result').setTemplate($("#TemplateResultsTable").html());
$("#result").processTemplate(data);

This is my results template

<script type="text/html" id="TemplateResultsTable">    
<h3>{$T[0].StatusName} - Found: {$T.length} </h3>
<table>
    <tr>
        <th>Name</th>
        <th>Description</th>
        <th>Date Processed</th>
    </tr>
    {#foreach $T as match}
        <tr>
            <td>{$T.match.Title}</td>
            <td>{$T.match.Description}</td>
            <td>{$T.match.DateProcessed}</td>
        </tr>
    {#/for}
</table>
</script>

All works great apart from the fact that my date is outputted on the page as /Date(1258125238090)/

How do I format the date inside my results template?

jamesaharvey
  • 14,023
  • 15
  • 52
  • 63
Rippo
  • 22,117
  • 14
  • 78
  • 117

2 Answers2

5

Answer below in case anybody else searches this post...

Add the following JScript....

function formatJSONDate(jsonDate) {
    var date = eval(jsonDate.replace(/\/Date\((\d+)\)\//gi, "new Date($1)"));
    return dateFormat(date, "ddd ddS mmm yyyy");
 }  

download a javascript date format library then in your jTemplate template add

<td>{formatJSONDate($T.match.DateProcessed)}</td>

And that's that!

Rippo
  • 22,117
  • 14
  • 78
  • 117
1

I ran into this same problem and ended up (after much aggravation) just returning the date as a string in my JSON.

jamesaharvey
  • 14,023
  • 15
  • 52
  • 63
  • Looks like I have figured it out, quite easy when you get the format right..... will post an answer soon – Rippo Nov 17 '09 at 15:14