0

I have a C# code to get some dates from a Sql Table, these dates are filled by mssql getdate() the problem here is that my dates are displayed in the following structure:

/Date(1480343496517)/

And what I expect is this:

2016-11-28 08:13:23.820 //This is what I get executing my query in MSSQL

This is my full code:

C# Class

public MyDate(DateTime date)
{
    Date = date;
}
public DateTime Date { get; set; }

WebMethod

[WebMethod]
public string ShowDate()
{
    DataTable dt = new DataTable();
    dt = conn.CheckTable("date");
    MyDate fch;

    List<MyDate> list = new List<MyDate>();

    for (int i = 0; i < dt.Rows.Count; i++)
    {
        fch = new Fecha();
        fch.Date = Convert.ToDateTime(dt.Rows[i]["Date"]);

        list.Add(fch);
        fch = null;
    }

    JavaScriptSerializer js = new JavaScriptSerializer();
    string lines = js.Serialize(list);
    return lines;
}

CheckTable

public DataTable CheckTable(string table)
{
    dt = new DataTable();
    ds = new DataSet();
    string sql = "";

    switch (table)
    {
        case "date":
            sql = "SELECT Date FROM dbo.Table_1";
            break;
    }

    try
    {
        Open();
        SqlDataAdapter da = new SqlDataAdapter(sql, conn);
        da.Fill(ds);
        dt = ds.Tables[0];
    }
    catch (Exception ex)
    {

    }
    finally
    {
        Close();
    }
    return dt;
}

And my JS function:

$.ajax({
    type: 'POST', //POST | GET
    url: "WebService.asmx/ShowDate", //Direccion del ws
    dataType: 'json',
    contentType: 'application/json',
    timeout: 600000,
    error: function (xhr) {
      $('#dates').html('<option>Error</option>');
    },
    success: function (data) {
         var datas = JSON.parse(data.d);
         for (var i = 0; i < datos.length; i++) {
         var myDate= datas[i].Date;

         options += '<option value ="' + myDate+ '">';
         options += myDate;
         options += '</option>';
         }
         $('#dates').html(options);
     }
});

What I'm doing wrong and what can I do to solve it?

Daniel A. White
  • 187,200
  • 47
  • 362
  • 445
Miguel Flores
  • 137
  • 4
  • 12
  • I'm guessing the JavaScriptSerializer converts the dateTime to milliseconds so it can easily be sent to the client as JSON – adeneo Nov 28 '16 at 14:47

1 Answers1

0

I don't know what the other classes you're using are, but if you just want a list of dates from the query then this should do it...

[WebMethod]
public string ShowDate()
{
    DataTable dt = new DataTable();
    dt = conn.CheckTable("date");

    List<DateTime> list = new List<DateTime>();

    for (int i = 0; i < dt.Rows.Count; i++)
    {
        list.Add(Convert.ToDateTime(dt.Rows[i]["Date"]).ToString());
    }

    JavaScriptSerializer js = new JavaScriptSerializer();
    string lines = js.Serialize(list);
    return lines;
}

then in the JavaScript, change this one line...

var myDate = datas[i];

The web method will now just return a serialised list of dates, so no need to get a .date property of the list objects any more.

Reinstate Monica Cellio
  • 25,975
  • 6
  • 51
  • 67