简体   繁体   English

如何在asp.net中获取带有秒的Datetimepicker?

[英]How to get Datetimepicker with seconds in asp.net?

i have a task that i have to filter the records based on DateFrom and DateTo. 我有一个任务,我必须根据DateFrom和DateTo过滤记录。 in mysql the date fromat is in '2014-07-18 20:03:08' like this. 在mysql中fromatat的日期是在'2014-07-18 20:03:08'这样的。 In front end i used DatetimePicker to textbox..datetimepicker format results gives '2014/07/09 14:00'. 在前端,我将DatetimePicker用于文本框。datetimepicker格式的结果为“ 2014/07/09 14:00”。 so not possible for compare db dateformat and datetime formate. 因此无法比较db dateformat和datetime formate。

i want datetime picker with seconds..plz help me 我想要带有秒的日期时间选择器..plz帮助我

A DateTimePicker stores a DateTime in the Value property. DateTimePicker存储DateTimeValue属性。 You can add desired seconds to it: 您可以在其中添加所需的秒数:

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

datetimepicker format results gives '2014/07/09 14:00'. datetimepicker格式的结果为“ 2014/07/09 14:00”。

Don't use strings as parameters for datetime-columns in MySql. 不要在MySql中使用字符串作为datetime列的参数。 Instead use sql-parameters and pass the DateTime directly: 而是使用sql-parameters并直接传递DateTime

// 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());
    }
}

DateTime pickers aren't always made for this accuracy. 并非总是出于这种准确性而使用DateTime选择器。

I suggest using a customized DateTime picker with textboxes to allow your users to input complete time in seconds. 我建议使用带有文本框的自定义DateTime选择器,以允许您的用户以秒为单位输入完整时间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM