How to get Datetimepicker with seconds in asp.net?

Total Post:412

Points:2888
C#
 1619  View(s)
Ratings:
Rate this:

I have a task that i have to filter the records based on DateFrom and DateTo. in mysql the date fromat is in '2014-07-18 20:03:08' like this. In front end i used DatetimePicker to textbox..datetimepicker format results gives '2014/07/09 14:00'. so not possible for compare db dateformat and datetime formate.

i want datetime picker with seconds..plz help me

  1. Post:149

    Points:1043
    Re: How to get Datetimepicker with seconds in asp.net?

    A DateTimePicker stores a DateTime in the Value property. You can add desired seconds to it:

    DateFrom.Value = DateFrom.Value.AddSeconds(seconds);

    datetimepicker format results gives '2014/07/09 14:00'.

    Don't use strings as parameters for datetime-columns in MySql. Instead use sql-parameters and pass the DateTime directly:

    // add seconds if you want, but i don't see any reason for it
    // maybe you want to include the To-date, then you can add a day - one second
    DateTo.Value = DateTo.Value.AddDays(1).AddSeconds(-1);
     
    using (MySqlConnection conn = new MySqlConnection("connStr"))
    {
        try
        {
            conn.Open();
     
            string sql = @"SELECT t.From, t.To, OtherColumns...
                           FROM TableName t
                           WHERE FROM >= @From AND To <= @To";
            using(MySqlCommand cmd = new MySqlCommand(sql, conn))
            {
                cmd.Parameters.AddWithValue("@From", DateFrom.Value);
                cmd.Parameters.AddWithValue("@To", DateTo.Value);
                using (MySqlDataReader reader = cmd.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        // ...
                    }
                }
            }
     
        } catch (Exception ex)
        {
            Console.WriteLine(ex.ToString());
        }
    }

     

      Modified On Apr-07-2018 06:29:23 AM

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!