简体   繁体   English

如何仅从Windows窗体DateTimePicker控件中获取日期值?

[英]How to get only the date value from a Windows Forms DateTimePicker control?

I'm building an application with C# code. 我正在使用C#代码构建应用程序。
How do I get only the date value from a DateTimePicker control? 如何仅从DateTimePicker控件获取日期值?

I'm assuming you mean a datetime picker in a winforms application. 我假设您是指Winforms应用程序中的日期时间选择器。

in your code, you can do the following: 在您的代码中,您可以执行以下操作:

string theDate = dateTimePicker1.Value.ToShortDateString();

or, if you'd like to specify the format of the date: 或者,如果您想指定日期格式:

string theDate = dateTimePicker1.Value.ToString("yyyy-MM-dd");
DateTime dt = this.dateTimePicker1.Value.Date;

I had this issue when inserting date data into a database, you can simply use the struct members separately: In my case it's useful since the sql sentence needs to have the right values and you just need to add the slash or dash to complete the format, no conversions needed. 在将日期数据插入数据库时​​,我遇到了这个问题,您可以单独使用struct成员:就我而言,这很有用,因为sql语句需要具有正确的值,而您只需要添加斜杠或破折号即可完成格式,无需转换。

DateTimePicker dtp = new DateTimePicker();
String sql = "insert into table values(" + dtp.Value.Date.Year + "/" + 
dtp.Value.Date.Month + "/" + dtp.Value.Date.Day + ");";

That way you get just the date members without time... 这样一来,您只需要时间即可获得约会成员...

string shortDate = dateTimePicker1.Value.ToShortDateString();

You mean how to get date without the time component? 您的意思是如何在没有时间成分的情况下获取日期? Use DateTimePicker.Value.Date But you need to format the output to your needs. 使用DateTimePicker.Value.Date但是您需要根据需要格式化输出。

@Shoban It looks like the question is tagged c# so here is the appropriate snipped http://msdn.microsoft.com/en-us/library/system.windows.forms.datetimepicker.value.aspx @Shoban看来问题被标记为c#,所以这里是适当的摘录http://msdn.microsoft.com/zh-cn/library/system.windows.forms.datetimepicker.value.aspx

public MyClass()
{
    // Create a new DateTimePicker
    DateTimePicker dateTimePicker1 = new DateTimePicker();
    Controls.Add(dateTimePicker1);
    MessageBox.Show(dateTimePicker1.Value.ToString());

    dateTimePicker1.Value = DateTime.Now.AddDays(1);
    MessageBox.Show(dateTimePicker1.Value.ToString());
 } 

Following that this question has been already given a good answer, in WinForms we can also set a Custom Format to the DateTimePicker Format property as Vivek said, this allow us to display the date/time in the specified format string within the DateTimePicker , then, it will be simple just as we do to get text from a TextBox . 在此问题已经得到很好的回答之后,在WinForms中,我们还可以像Vivek所说的那样将Custom Format设置为DateTimePicker Format属性,这使我们可以在DateTimePicker中以指定的格式字符串显示日期/时间,就像我们从TextBox获取文本一样,这将很简单。

// Set the Format type and the CustomFormat string.
dateTimePicker1.Format = DateTimePickerFormat.Custom;    
dateTimePicker1.CustomFormat = "yyyy/MM/dd";

dateTimePicker1

We are now able to get Date only easily by getting the Text from the DateTimePicker : 现在,我们只能通过从DateTimePicker获取文本来轻松获取Date:

MessageBox.Show("Selected Date: " + dateTimePicker1.Text, "DateTimePicker", MessageBoxButtons.OK, MessageBoxIcon.Information);

在此处输入图片说明

NOTE: If you are planning to insert Date only data to a date column type in SQL, see this documentation related to the supported String Literal Formats for date . 注意:如果您打算在SQL中将“仅日期”数据插入到日期列类型中,请参阅此文档 ,该文档date的受支持的字符串文字格式有关。 You can not insert a date in the format string ydm because is not supported: 您不能以格式字符串ydm插入日期,因为它不受支持:

dateTimePicker1.CustomFormat = "yyyy/dd/MM";  
var qr = "INSERT INTO tbl VALUES (@dtp)";
using (var insertCommand = new SqlCommand..
{
   try
   {
     insertCommand.Parameters.AddWithValue("@dtp", dateTimePicker1.Text);
     con.Open();
     insertCommand.ExecuteScalar();
   }
   catch (Exception ex)
   {
      MessageBox.Show("Exception message: " + ex.Message, "DateTimePicker", MessageBoxButtons.OK, MessageBoxIcon.Error);
   }

the above code ends in the following Exception: 上面的代码以以下异常结尾: 在此处输入图片说明

Be aware. 意识到。 Cheers. 干杯。

Try this 尝试这个

var store = dtpDateTimePicker.Value.Date;

store can be anything entity object etc. store可以是任何实体对象等。

Datum = DateTime.Parse(DateTimePicker1.Value.ToString("dd/MM/yyyy"))

暂无
暂无

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

相关问题 如何在Windows应用程序的dateTimePicker控件中从Sql Server中获取保存的日期值 - How to get saved date value from Sql Server in dateTimePicker Control in Windows Application 如何在 C# Windows Forms DateTimePicker 中将向上/向下控件控制为只有几分钟? - How do I control the up/down controls to only minutes in C# Windows Forms DateTimePicker? 如何从 datetimepicker 仅获取日期的分隔值? - How to get from datetimepicker only the separated values of the date? 仅在DatetimePicker中获取日期 - get only Date in DatetimePicker 如何在 (.NET) 中将 datetimepicker 值设置为仅日期 - How to set datetimepicker value to only date in (.NET) 如何通过Windows窗体中的dateTimePicker将日期插入SQL Server? - How to insert date into sql server through dateTimePicker in windows forms? 如何在Windows窗体中使用C#将选定的DateTimePicker值获取到Excel单元格 - How can i get the selected DateTimePicker Value to the Excel cell using C# in Windows Forms 如何将日期单元格 datagridview 的值放入 datetimepicker? - How to get the value of date cell datagridview into the datetimepicker? 如何获取带有日期和当前时间的datetimepicker值 - how to get datetimepicker value with Date and current time C# Windows Forms DateTimePicker,如何获取SelectedText、SelectionStart和SelectionLength? - C# Windows Forms DateTimePicker, how to get the SelectedText, SelectionStart, and SelectionLength?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM