如何从 Windows 窗体 DateTimePicker 控件中仅获取日期值?
我正在使用 C# 代码构建一个应用程序。
如何从 DateTimePicker
控件中仅获取日期值?
I'm building an application with C# code.
How do I get only the date value from a DateTimePicker
control?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
我假设您指的是 winforms 应用程序中的日期时间选择器。
在您的代码中,您可以执行以下操作:
或者,如果您想指定日期的格式:
I'm assuming you mean a datetime picker in a winforms application.
in your code, you can do the following:
or, if you'd like to specify the format of the date:
我在将日期数据插入数据库时遇到了这个问题,您可以简单地单独使用结构成员:
就我而言,它很有用,因为 sql 语句需要具有正确的值,您只需添加斜杠或破折号即可完成格式,无需转换。
这样你就可以得到约会成员而无需时间......
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.
That way you get just the date members without time...
既然这个问题已经得到了很好的答案,在WinForms中我们还可以设置一个 自定义格式到 DateTimePicker Format 属性如 Vivek 所说,这允许我们显示日期/时间在 DateTimePicker 中指定格式字符串,然后,就像我们从 TextBox 中获取文本一样简单。
我们现在只能通过从 DateTimePicker 获取文本来轻松获取日期:
< img src="https://i.sstatic.net/pUEWZ.png" alt="在此处输入图像描述">
注意:如果您计划将仅日期数据插入到日期列类型在 SQL 中,请参阅此文档与日期支持的字符串文字格式相关。 您无法在格式字符串 ydm 中插入日期,因为不受支持:
以上代码以以下异常结束:
请注意。 干杯。
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.
We are now able to get Date only easily by getting the Text from the DateTimePicker:
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. You can not insert a date in the format string ydm because is not supported:
the above code ends in the following Exception:
Be aware. Cheers.
您的意思是如何获取没有时间部分的日期? 使用 DateTimePicker.Value.Date
但您需要根据您的需要格式化输出。
You mean how to get date without the time component? Use DateTimePicker.Value.Date
But you need to format the output to your needs.
@Shoban 看起来这个问题被标记为 c# 所以这里是适当的片段
http://msdn.microsoft.com/ en-us/library/system.windows.forms.datetimepicker.value.aspx
@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
尝试这个
商店可以是任何实体对象等。
Try this
store can be anything entity object etc.
简单,喜欢
Easy, like it