469,963 Members | 1,223 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,963 developers. It's quick & easy.

DateTime comparasion problem with 0:00:00

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

Nov 16 '07 #1
2 2897
"Mike" <ab******@yahoo.comwrote in message
news:q0********************************@4ax.com...
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.
Nov 16 '07 #2
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).
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Alberto Poblacion" <ea******************************@poblacion.orgwro te
in message news:Ow**************@TK2MSFTNGP03.phx.gbl...
"Mike" <ab******@yahoo.comwrote in message
news:q0********************************@4ax.com...
>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.

Nov 16 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Sorisio, Chris | last post: by
15 posts views Thread by Dan S | last post: by
9 posts views Thread by Tom | last post: by
9 posts views Thread by Phil B | last post: by
39 posts views Thread by Frederick Gotham | last post: by
8 posts views Thread by Karl Groves | last post: by
10 posts views Thread by Jeff | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.