tags:

views:

606

answers:

3

I'm filtering a datatable on a date range using DataTable.Select, my criteria string is:

"CreatedOn >='03/11/2009 00:00:00' AND CreatedOn <='03/11/2009 23:59:00'"

This filter returns no rows (even though I can see matching rows in the unfiltered datatable). However I notice if I change the criteria to (note the day/month transposition):

"CreatedOn >='11/03/2009 00:00:00' AND CreatedOn <='11/03/2009 23:59:00'"

The datatable filters as expected. Clearly this seems to be a date localisation issue, is there an easy way to format the date to avoid this issue?

+4  A: 

Use a standard ISO format datetime like this (for 3rd Nov):

"CreatedOn >='2009-11-03 00:00:00' AND CreatedOn <='2009-11-03 23:59:00'"

In fact, if you want all records created on 3rd Nov, you should really do this as you also want records created in that last minute e.g. 23:59:30:

"CreatedOn >='2009-11-03' AND CreatedOn < '2009-11-04'"

There's a quick reference here

AdaTheDev
Perfect, thanks.
Simon
+1  A: 

See if you can format you date to "dd MMM yyyy" rather than "MM/dd/yyyy".

something like this

dt.Select(String.Format("col >= '{0}'", new DateTime(2008, 12, 11).ToString("dd MMM yyyy")))
astander
+1  A: 

Try this:

string selectString = String.Format("CreatedOn >= '{0}' AND CreatedOn <= '{1}'",
                           startDate.ToString(DateTimeFormatInfo.InvariantInfo), 
                           endDate.ToString(DateTimeFormatInfo.InvariantInfo));
dt.Select(selectString);
bniwredyc