Search code examples
c#javascriptjsonwcfdate

Convert JSON "/Date(1404860400000)/" to javascript date


so I am currently creating a webpage that uses data from an Oracle database. I am retrieving a list of data from the database and displaying it on the webpage. The problem is that when retrieving the data, the dates which are located in the list come back as JSON dates.

So whenever I retrieve a date from the database using JSON and try to display it on my webpage, its shown in this format: "/Date(1404860400000)/"

How can I convert this into a date like "dd-mm-yy":"21-AUG-14"?

My current code is like so:

JavaScript - For formatting the data and displaying in a HTML table

  var AuditHTML = "<table class='tablesorter full_width_table info_table_style ui-body-d ui-shadow table-stripe ui-responsive' id='auditTable'>" +
                    "<thead >" +
                       "<tr class='ui-bar-b schedule_row '>" +
                         "<th>ID</th>" +
                         "<th>User ID</th>" +
                         "<th>Action</th>" +
                         "<th>Date</th>" +
                         "<th>App ID</th>" +
                         "<th>Device ID</th>" +
                         "<th>Notes</th>" +
                       "</tr>" +
                     "</thead>" +
                     "<tbody>";


        for (s = 0; s < auditList.length; s++) {
            if (auditList[s].Date <= loggingto && auditList[s].Date >= loggingfrom) {
                AuditHTML += "<tr class='schedule_row display' id='auditTr_" + s + "'>" +
                          "<td> " + auditList[s].ID + "</td>" +
                          "<td> " + auditList[s].UserID + "</td>" +
                          "<td> " + auditList[s].Action + "</td>" +
                          "<td> " + auditList[s].Date + "</td>" +
                          "<td> " + auditList[s].AppID + "</td>" +
                          "<td> " + auditList[s].DeviceID + "</td>" +
                          "<td class='note'> " + auditList[s].Notes + "</td>";
                AuditHTML += "</tr>";
            }
        }
       AuditHTML += "</tbody></table>";

       $("#auditContent").html(AuditHTML);

HTML - to display table

 <div id="auditContent">
        </div>

Thanks for your time/help!


Solution

  • Solved it myself using JQuery moment:

    Created a method to run each JSON date through:

    function parseJsonDate(jsonDateString) {
        return moment(jsonDateString).format("D-MMM-YY").toUpperCase();
    }
    

    And called it when rendering the table:

    "<td> " + parseJsonDate(auditList[s].Date) + "</td>" +