一个非常简单的问题:如何在c#中从windows形式的datetimepicker中仅获取日期 [英] A very easy question: How to Fetch only date from datetimepicker in windows form in c#

查看:303
本文介绍了一个非常简单的问题:如何在c#中从windows形式的datetimepicker中仅获取日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序,它将日期和时间保存到sql server 2008数据库中,而不是在所选时间范围内提取这些值,即从Date和To日期。查询搜索数据库,如果今天的日期介于所选日期范围之间,如果是,则执行任务。

问题是如果我使用datetimepicker.value.date,那么它还提供包括日期和日期在内的完整值我不希望时间在这里的时间。并且当要检查它是否在数据库中的指定日期范围内时,它不能在字符串中更改。我甚至尝试了自定义格式,也给了它给予时间。

请帮助我的朋友......



摘要:我只有想从datetimepicker获取日期(dd / MM / yyyy)而不将其更改为字符串。

I have a application which saves date and time into sql server 2008 database, than those values are fetched within the selected time range i.e, from Date and To date. The query searches into database that if today's date lie between the selected date range, if yes than perform task.
The issue is if I use datetimepicker.value.date then also its giving full value including date and time where I dont want time part here. And as its to be checked for falling in specified range of date in database it can't be changed in string. I have even tried the custom format than also its giving time also.
Kindly help me out friends...

Summary: I only want to get date(dd/MM/yyyy) from datetimepicker without changing it to string.

推荐答案

.NET中的DateTime对象总是有时间 - 它们存储为从任意时间点起几毫秒,所以除非你创建一个自定义类,否则没有时间没有TimeTime。



但是,SQL确实如此有一个不包括时间的日期类型。您可以在数据库中使用它,并且应该忽略tIme部分。



但是,我不会 - 我会继续使用DateTime值,因为如果你总是使用Date属性,那么你所有的值都将基于午夜,所以无论如何它都会正常工作......
DateTime objects in .NET always have a time - they are stored as a number of milliseconds since an arbitrary point in time, so there is no way to have a DateTime without the Time unless you create a custom class.

However, SQL does have a Date type which does not include the time. You could use that in your DB, nd the tIme portion should be ignored.

However, I wouldn't - I'd continue with the DateTime values, because if you always use the Date property, then all your values will be midnight based, so it will just work anyway...


DateTime dt = this.dateTimePicker1.Value.Date;



但这也包含 12:00 AM 的默认时间,因为它是 DateTime 对象。



否则你必须转换成字符串。


But this will also contain default time that is 12:00 AM as it is a DateTime Object.

Otherwise you have to convert to string.


试试通过设置CustomFormat

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



参考: http://stackoverflow.com/questions / 1138195 / get-date-from-date-time-picker [ ^ ]

希望这样可行。


Refer:http://stackoverflow.com/questions/1138195/get-date-from-date-time-picker[^]
Hope this will work.


这篇关于一个非常简单的问题:如何在c#中从windows形式的datetimepicker中仅获取日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆