DateTime comparasion problem with 0:00:00

M

Mike

Hi,

I have DateTime filed in db table. I'd like to search my db based on
DateTime filed, actually to list all rows with particular date, for
e.g. 16.11.2007...

I send string to method for. eg. 16.11.2007 --> short date and convert
that to DateTime. After conversion it looks like 16.11.2007 0:00:00

How I can get short date like 16.11.2007 without 0:00:00 after
conversion with Convert.ToDateTime(MyDate)

I have tried
Convert.ToDateTime(MyDate).Date
without success

How I can do that?

Thanks
 
A

Alberto Poblacion

Mike said:
I have DateTime filed in db table. I'd like to search my db based on
DateTime filed, actually to list all rows with particular date, for
e.g. 16.11.2007...

I send string to method for. eg. 16.11.2007 --> short date and convert
that to DateTime. After conversion it looks like 16.11.2007 0:00:00

How I can get short date like 16.11.2007 without 0:00:00 after
conversion with Convert.ToDateTime(MyDate)

I have tried
Convert.ToDateTime(MyDate).Date
without success

How I can do that?

You can't. DateTime is defined in the Framework as a struct that contains
numeric values for the year, month, day, hour, minute and second. Those are
_numbers_, they need to contain some value, even if it is a zero, so the
DateTime struct, and therefore anything that you convert to DateTime, will
always contain the zeroes.

A different thing is displaying a string representation of the values
contained inside the DateTime struct. In this case, when you perform the
conversion from DateTime to String, you can specify a format that omits the
time part. For instance:

DateTime someDate = new DateTime(2007, 11, 16);
string theTextOfMyDate = someDate.ToString("dd.MM.yyyy");

The preceding will store "16.11.2007" in the string variable.
 
N

Nicholas Paldino [.NET/C# MVP]

It might also be easier to just let the database handle it. Assuming
one is using SQL Server, you could trim the time from the datetime value in
SQL Server (and if you are using SQL Server 2008 in beta, I'm sure there is
a conversion method to convert specifically to the date portion, since there
are separate types for date and time now) and then query for all the values
in the table where the date equals that date (applying the same
transformation to the values on the table, if they have time values).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top