locked
How to i convert my Date from my database to a String RRS feed

  • Question

  • User1391648435 posted
    DataTable dt = new DataTable();
    using (MySqlConnection con = new MySqlConnection("server=localhost;user id=root;password=root;database=impgarden;"))
    {
    con.Open();
    MySqlCommand cmd = new MySqlCommand("SELECT Id,Temperature,ReadDateTime FROM smartterranium WHERE ReadDateTime BETWEEN @ViewDateFrom and @ViewDateTo group by ReadDateTime ", con);
    cmd.Parameters.AddWithValue("@ViewDateFrom", tbViewDateFrom.Text);
    cmd.Parameters.AddWithValue("@ViewDateTo", tbViewDateTo.Text);
    
    MySqlDataAdapter da = new MySqlDataAdapter(cmd);
    da.Fill(dt);
    con.Close();
    
    object[,] chartValues = new object[dt.Rows.Count,2];
    for (int i = 0; i < dt.Rows.Count; i++)
    {
    //category = category + "," + dt.Rows[i]["ReadDateTime"].ToString();
    //chartValues[i] = Convert.ToDecimal(dt.Rows[i]["Temperature"]);
    //chartValues[i, 0] = dt.Rows[i]["ReadDateTime"];
    chartValues[i, 0] = dt.Rows[i]["ReadDateTime"].ToString();
    chartValues[i, 1] = dt.Rows[i]["Temperature"];
    }
    DotNet.Highcharts.Highcharts chart = new DotNet.Highcharts.Highcharts("chart").InitChart(new Chart { DefaultSeriesType = ChartTypes.Line })
    .SetTitle(new Title
    {
    Text = "Temperature",
    X = -20
    })
    .SetXAxis(new[]
    {
    new XAxis
    { 
    Type = AxisTypes.Datetime,
    
    },
    })


    I having problems trying in to parse in the date i want from my database, the format is eg, 23/03/2017. My database name for date is ReadDateTime.
    Is there a way to do it?

    Thursday, March 30, 2017 3:04 AM

All replies

  • User-271186128 posted

    Hi JovanYong,

    How to i convert my Date from my database to a String

    chartValues[i, 0] = dt.Rows[i]["ReadDateTime"].ToString();

    I test your code, it seems that everything works well, please explain more details about your problem.

    Here are some relevant articles about Datetime transform, you could refer to them:

    https://msdn.microsoft.com/en-us/library/8kb3ddd4(v=vs.110).aspx

    https://msdn.microsoft.com/en-us/library/cc165448.aspx

    Best regards,
    Dillion

    Friday, March 31, 2017 6:28 AM
  • User-1716253493 posted

    What is the problem?

    To format the date to spesific format you can use ToString("dd/MM/yyyy")

    Try this

    chartValues[i, 0] = Convert.ToDateTime(dt.Rows[i]["ReadDateTime"]).ToString("dd/MM/yyyy");

    Friday, March 31, 2017 7:21 AM