c# - Convert JSON "Date(1404860400000)" to javascript date - Stack Overflow

so I am currently creating a webpage that uses data from an Oracle database. I am retrieving a list of

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 e 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!

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 e 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!

Share Improve this question edited Aug 21, 2014 at 14:52 Mimi Lauren asked Aug 21, 2014 at 12:57 Mimi LaurenMimi Lauren 8002 gold badges6 silver badges12 bronze badges 3
  • 1 Jeezes, that's milliseconds from epoch, and all you do is new Date(1404860400000) to get a javascript date – adeneo Commented Aug 21, 2014 at 13:03
  • create a javascript function like function parseJsonDate(jsonDateString) { return new Date(parseInt(jsonDateString.replace('/Date(', ''))); } and call it where you assigning date. – CodeGuru Commented Aug 21, 2014 at 13:06
  • When creating a function like that and passing the json dates to it, they all show as Thu Jan 01 1970 00:00:00 GMT+0000 (GMT Daylight Time) – Mimi Lauren Commented Aug 21, 2014 at 13:32
Add a ment  | 

3 Answers 3

Reset to default 2

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>" +

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./jsref/jsref_obj_date.asp

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745655491a4638520.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信