Reputation: 800
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!
Upvotes: 2
Views: 9975
Reputation: 29
It can be as bellow example too.
function formatDateFromMilliseconds(value) {
// receives in milliseconds 853113600000
// returns dd/mm/yyyy
let d = new Date(value).toISOString().substring(0, 19);
return d.substring(8, 10)+ '/' + d.substring(5, 7) + '/' + d.substring(0, 4);
}
Upvotes: -1
Reputation: 800
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>" +
Upvotes: 2
Reputation: 70
Look at the JavaScript date object. You can pass a date in milliseconds into its constructor.
var d = new Date(1404860400000);
You could then call the various get methods to return the date in the format you want.
http://www.w3schools.com/jsref/jsref_obj_date.asp
Upvotes: 0