Click here to Skip to main content
15,901,426 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, I have some values return from my controller written in C#. I have debug the codes and all data returned is ok. But when I try to display the data in HTML, the data appears weird for all those which are not in String data type.
For example, if I have date time which is in 2016-09-27 then it would display as /Date(123456789)/.
Below are my script in javascript format:
JavaScript
PullRecordsForEfficiencyTable: function(firstName, customerId){
    firstName = firstName;
    customerId = customerId;

    $.ajax({
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        type: "POST",
        url: "/Customer/LoadRecordsForEfficiencyTable",
        data: JSON.stringify({
              firstName :firstName;
              customerId : customerId;

        }),
        success: function (data) {
            document.getElementById('dvFirstName').innerHTML = data.data[0].Name;
            document.getElementById('dvStartDate').innerHTML = data.data[0].StartDate;
        },
        fail: function (jqXHR, textStatus, errorThrown) {
            alert(errorThrown);
        },
        error: function (jqXHR, textStatus, errorThrown) {
            alert(errorThrown);
        }

    });
},



The "FirName" appear perfectly but for "StartDate", it would be displayed as /Date(123456789)/.
Any ideas?

What I have tried:

1. Tried to search online but without getting any clues.
Posted
Updated 27-Sep-16 0:04am

1 solution

try this

JavaScript
function formatDate(date) {
         var d = new Date(date),
             month = '' + (d.getMonth() + 1),
             day = '' + d.getDate(),
             year = d.getFullYear();

         if (month.length < 2) month = '0' + month;
         if (day.length < 2) day = '0' + day;

         return [year, month, day].join('-');
     }


JavaScript
document.getElementById('dvStartDate').innerHTML = formatDate(new Date(parseInt(data.data[0].StartDate.substr(6))));
 
Share this answer
 
Comments
Jamie888 27-Sep-16 6:24am    
Sir, may I know what is the substr(6) for?
Karthik_Mahalingam 27-Sep-16 6:37am    
to remove /Date(
Jamie888 27-Sep-16 6:40am    
I have check online, does javascript would display any datetime as /Date(123456789)/ if we do not change its value as suggested by sir on above?
Karthik_Mahalingam 27-Sep-16 6:46am    
if the ajax json is passed as datetime object, it will be displayed like this only.
Jamie888 27-Sep-16 6:49am    
I have modified your script into:
return [month, day, year].join('/') + " " + [hour, minute, seconds].join(':');

How can I add "AM" or "PM" into it?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900