Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DataTable.Select date format problem

Tags:

.net

datatable

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?

like image 847
Simon Avatar asked Dec 08 '09 09:12

Simon


3 Answers

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

like image 127
AdaTheDev Avatar answered Sep 25 '22 21:09

AdaTheDev


Try this:

string selectString = String.Format("CreatedOn >= '{0}' AND CreatedOn <= '{1}'",
                           startDate.ToString(DateTimeFormatInfo.InvariantInfo), 
                           endDate.ToString(DateTimeFormatInfo.InvariantInfo));
dt.Select(selectString);
like image 20
bniwredyc Avatar answered Sep 21 '22 21:09

bniwredyc


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")))
like image 28
Adriaan Stander Avatar answered Sep 23 '22 21:09

Adriaan Stander