C# 如何仅从 Windows 窗体 DateTimePicker 控件中获取日期值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1138195/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get only the date value from a Windows Forms DateTimePicker control?
提问by srinivas
I'm building an application with C# code.
How do I get only the date value from a DateTimePicker
control?
我正在使用 C# 代码构建应用程序。
如何仅从DateTimePicker
控件中获取日期值?
采纳答案by tardomatic
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");
回答by Keith
DateTime dt = this.dateTimePicker1.Value.Date;
回答by waqasahmed
string shortDate = dateTimePicker1.Value.ToShortDateString();
回答by Vivek
You mean how to get date without the time component? Use DateTimePicker.Value.DateBut you need to format the output to your needs.
您的意思是如何在没有时间组件的情况下获取日期?使用DateTimePicker.Value.Date但是您需要根据需要格式化输出。
回答by jon37
@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/en-us/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());
}
回答by Gustave Santiago
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.
我在将日期数据插入数据库时遇到了这个问题,您可以简单地单独使用结构成员:在我的情况下,它很有用,因为 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...
这样你就可以在没有时间的情况下获得约会成员......
回答by WhySoSerious
Following that this question has been already given a good answer, in WinForms we can also set a Custom Formatto the DateTimePickerFormatproperty 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,我们还可以设置自定义格式到的DateTimePicker格式属性作为维韦克说,这让我们在中显示在指定格式字符串的日期/时间的DateTimePicker,那么,就像我们从TextBox获取文本一样简单。
// Set the Format type and the CustomFormat string.
dateTimePicker1.Format = DateTimePickerFormat.Custom;
dateTimePicker1.CustomFormat = "yyyy/MM/dd";
We are now able to get Date only easily by getting the Text from the DateTimePicker:
我们现在可以通过从DateTimePicker获取 Text 来轻松获取 Date :
MessageBox.Show("Selected Date: " + dateTimePicker1.Text, "DateTimePicker", MessageBoxButtons.OK, MessageBoxIcon.Information);
NOTE: If you are planning to insert Date only data to a datecolumn type in SQL, see this documentationrelated to the supported String Literal Formats for date. You can not insert a date in the format string ydmbecause is not supported:
注意:如果您计划将仅 Date 数据插入SQL 中的日期列类型,请参阅与date支持的字符串文字格式相关的文档。您不能在格式字符串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.
意识到。干杯。
回答by Mohit Arora
Try this
尝试这个
var store = dtpDateTimePicker.Value.Date;
store can be anything entity object etc.
store 可以是任何实体对象等。
回答by Joop
Datum = DateTime.Parse(DateTimePicker1.Value.ToString("dd/MM/yyyy"))