Click here to Skip to main content
15,914,500 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i am developing win form application. problem is i want to find data from datetimepicker1.value to
datetimepicker2.value here for ex.

if i want to get data for Date between '3/19/2014' and '3/20/2014' i get all the values added up on 3/19/2014 but doesn't get values added on 3/20/2014
Posted
Comments
OriginalGriff 19-Mar-14 6:36am    
Possibly, because that's tomorrow and your computer isn't good at seeing the future...
After that is sorted, this is not a good question - we cannot work out from that little what you are trying to do.
Remember that we can't see your screen, access your HDD, or read your mind.
So, if you are using a database, tell us which. Show us the code that doesn't work, show us the data you are looking at. Tell us how you know there are entries that match!
Use the "Improve question" widget to edit your question and provide better information.

One of the possible problems is that you are getting full DateTime values from DateTimePicker components. In this case Time part of DateTime value can be significant.

Asuming that you are assigning values to variables for filtering purposes:
C#
var startDate = datetimepicker1.Value;
var endDate = datetimepicker2.Value;


probably you will get values:
startDate = 3/19/2014 09:13:35
endDate = 3/20/2014 09:13:35

(according to time when DateTimePicker components where initialized Time part can be different).

Solution is to extract only Date part and ignore Time part:
C#
var startDate = datetimepicker1.Value.Date;
var endDate = datetimepicker2.Value.Date;


then you will get values like this:
startDate = 3/19/2014 00:00:00
endDate = 3/20/2014 00:00:00

Next step is to filter your data in proper way. For example when you are using EF and LINQ to get some data from DB try this:

C#
var invoices = from inv in db.Invoices
               where inv.InvoiceDate.Date >= startDate
                  && inv.InvoiceDate.Date <= endDate
               select x;


Now you should get all values in selected period.

[Update]
If you are using database (such as MS SQL Server) for storage purposes then you can choose Date field type. It will automatically store only Date part. It's helpful when you don't need to store Time part.

Hope it helps you :)
 
Share this answer
 
v2
Comments
gggustafson 19-Mar-14 14:16pm    
Good answer. Can't say how many times I forgot about the Time part.
Marcin Kozub 19-Mar-14 15:38pm    
Yup, sometimes it's a nightmare ;)
using(DBDataContext db = new DBDataContext())
{
var obj = db.YOURTABL.where(w=> w.yourDate <= ToDate && w.yourDate <= FromDate).ToList();
}
 
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