最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

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

matteradmin4PV0评论

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);
}
Post a comment

comment list (0)

  1. No comments so far