Click here to Skip to main content
15,868,141 members
Please Sign up or sign in to vote.
3.40/5 (2 votes)
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.
Posted

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...
 
Share this answer
 
Comments
kool15th 17-Feb-14 2:25am    
I have tried it but a error comes "The conversion of a varchar data type to a smalldatetime data type resulted in an out-of-range value." I have given the datetimepicker value to short and accessing its value by datetimepicker.value.date;
My database has the datatype smalldatetime. Is the am/pm causing problem ?
OriginalGriff 17-Feb-14 4:39am    
Yes - SmallDateTime is always 24 hour, and includes no seconds at all.
kool15th 17-Feb-14 7:06am    
than how to do
select * from Student_Attendance where Date1 >=" + dateTimePicker_CRpt_FromDate.Value.Date + " AND Date <=" + dateTimePicker_CRpt_ToDate.Value.Date + " ");

the error now is :
Incorrect syntax near 'AM'.

what should i make the datatype of Date1 column in database and the datetimepicker's syntax
and also how to get the result?
OriginalGriff 17-Feb-14 7:28am    
Easy: stop concatenating strings to form SQL statements!
It's really a very, very bad idea - use Parametrized queries instead at all times.
Not only does your problem disappear, but your database is protected against SQL Injection Attacks which can damage or destroy your database very, very easily.
DateTime dt = this.dateTimePicker1.Value.Date;

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.
 
Share this answer
 
Comments
kool15th 17-Feb-14 2:21am    
Actually I required date separately and time separately Such as class attendance type. Time ranges for 4-5 classes on a particular date within the allotted time and date range. I first need to check for valid date than its corresponding time range.
Try by setting CustomFormat
C#
// Set the Format type and the CustomFormat string.
dateTimePicker1.Format = DateTimePickerFormat.Custom;
dateTimePicker1.CustomFormat = "yyyy/MM/dd";

Refer:http://stackoverflow.com/questions/1138195/get-date-from-date-time-picker[^]
Hope this will work.
 
Share this answer
 
v2
Comments
kool15th 17-Feb-14 2:15am    
Thanks Gitanjali, I have tried this too but the problem occurs, it still shows time stamp along with it.
First convert the date in string and give it a format as per your need and the convert it back to DateTime. See,
String to DateTime
C#
String MyString;
 MyString = "1999-09-01 21:34 PM";
 //MyString = "1999-09-01 21:34 p.m.";  //Depends on your regional settings

 DateTime MyDateTime;
 MyDateTime = new DateTime();
 MyDateTime = DateTime.ParseExact(MyString, "yyyy-MM-dd HH:mm tt", null);

DateTime to String
C#
//DateTime to String
MyDateTime = new DateTime(1999, 09, 01, 21, 34, 00);
String MyString;
MyString = MyDateTime.ToString("yyyy-MM-dd HH:mm tt");

For more reference, go here[^]

Good Luck :)

-KR
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900