JTemplates format Date of use asp.net mvc

I have the following JSON date returned from an MVC page, for example.

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

and I use JTemplates to process the data as shown below.

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

This is my result 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>

Everything works fine, except that my date is displayed on the page as / Date (1258125238090) /

How do I format a date inside my result template?

+3
source share
2 answers

Answer below if anyone else is looking for 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");
 }  

load the javascript date format library , then in the jTemplate template add

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

What is it!

+5
source

( ) JSON.

+1

Source: https://habr.com/ru/post/1723115/


All Articles