0

I want to fill the data in a gridview so on success call, I wrote the code for that. But in response I get date value as /Date(1518114600000)/ which seems something different.

Also for getting it right I changed the format according to my requirement. Like below

var createdDate = new Date(result.CREATED_DATE);
                 var createdDateRCOM = createdDate.getDate() + '/' + (createdDate.getMonth() + 1) + '/' + createdDate.getFullYear();

But still it comes like /Date(1518114600000)/

Here is my below code for what I wrote

function getDataForGrid(evt) {

        var ddlMZone = $('#ddlMaintenanceZone').val();

        $.ajax({
            type: "POST",
            url: "Dashboard.aspx/GetGridZoneData",
            data: JSON.stringify({ ddlMZone: ddlMZone }),
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (data) {

                var datVal = JSON.parse(data.d);

                var details = [];
                for (var i = 0, len = datVal.length; i < len; i++) {
                    var result = datVal[i];

                    if (result.APPROVED_FLG == 0)
                    {
                        result.APPROVED_FLG = "No";
                    }

                    var buttonColumn = "<b><a href='DashboardData.aspx?id=" + result.RJ_SAPID + "' target='_blank'>Update Info</a></b>";
                    details.push([result.RJ_SAPID, result.RJ_FACILITY_ID, result.SITE_NAME, result.LATITUDE, result.LONGITUDE, result.CREATED_DATE, result.STATUS, result.APPROVED_FLG, buttonColumn]);

                }

                $('#grdMZInfo').DataTable({
                    destroy: true,
                    "aaData": details,
                    "aoColumns": [
                        { "sTitle": "RJ SapId" },
                        { "sTitle": "Facility Id" },
                        { "sTitle": "Site Name" },
                        { "sTitle": "Latitude" },
                        { "sTitle": "Longitude" },
                        { "sTitle": "Created date" },
                        { "sTitle": "Status" },
                        { "sTitle": "Approved flag" },
                        { "sTitle": "Update Data" }
                    ],
                    "bDestroy": true
                });

            },
            error: function (data) {
                alert('Something went wrong..!!');
            }
        });
    }

Please suggest what is wrong here

update

[WebMethod]
    public static string GetGridZoneData(string ddlMZone)
    {
        DataTable dt = new DataTable();            
        try
        {
            CommonDB ObjCommon = new CommonDB();
            dt = ObjCommon.GetGridZoneDataForMZ(ddlMZone);
            return DataTableToJSON(dt);
        }
        catch (Exception)
        {
            throw;
        }

    }

    public static string DataTableToJSON(DataTable table)
    {
        JavaScriptSerializer jsSerializer = new JavaScriptSerializer();
        List<Dictionary<string, object>> parentRow = new List<Dictionary<string, object>>();
        Dictionary<string, object> childRow;
        foreach (DataRow row in table.Rows)
        {
            childRow = new Dictionary<string, object>();
            foreach (DataColumn col in table.Columns)
            {
                childRow.Add(col.ColumnName, row[col]);
            }
            parentRow.Add(childRow);
        }
        return jsSerializer.Serialize(parentRow);
    }
Nad
  • 4,605
  • 11
  • 71
  • 160
  • can you post the server code? – SilentCoder Feb 27 '18 at 07:32
  • 1
    Possible duplicate of [How to handle json DateTime returned from WCF Data Services (OData)](https://stackoverflow.com/questions/3818719/how-to-handle-json-datetime-returned-from-wcf-data-services-odata) – VDWWD Feb 27 '18 at 07:32
  • are you referring to createdDateRCOM object ? is that also as createdDate ? – G_S Feb 27 '18 at 07:34
  • /Date(1518114600000)/ is the JSON dateformat. If you are looking to get date from createdDate object, you can use eval(createdDate .replace(/\/Date\((\d+)\)\//gi, "new Date($1)")); regex to do that. – G_S Feb 27 '18 at 07:36
  • @G_S": yes I want to format date in `dd/mm/yy` how should I proceed – Nad Feb 27 '18 at 09:13
  • @SilentCoder: Posted the server code too. have a look – Nad Feb 27 '18 at 09:14
  • @BNN, are you formatting date at server side? or do you want to format it at client side? – G_S Feb 27 '18 at 10:29
  • @G_S: I want it on client side. – Nad Feb 27 '18 at 10:30
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/165887/discussion-between-g-s-and-bnn). – G_S Feb 27 '18 at 10:43

2 Answers2

1

For any date related calculations, I personally prefer moment js as it has many good features and we can easily manage dates. Its as simple as refering moment js file and using it.

In your case, we can just say moment('/Date(1224043200000)/').format('DD/MM/YY')

G_S
  • 7,068
  • 2
  • 21
  • 51
  • I tried like this, `moment(result.CREATED_DATE).format('DD/mm/YYYY')` but getting date something different such as `09/00/2018` – Nad Feb 27 '18 at 10:52
0

In server code I format my DateTime object to string like below,

YourObject obj = new YourObject {
     startDate = dta[i].startD.ToString("MM/dd/yyyy");
}

In my Jquery,

 var startDate =  new Date(YourObject.startDate),

Above approach help me to resolve your kind of issue. Hope this will help you.

SilentCoder
  • 1,970
  • 1
  • 16
  • 21