PC Review


Reply
Thread Tools Rate Thread

DateTime comparasion problem with 0:00:00

 
 
Mike
Guest
Posts: n/a
 
      16th Nov 2007
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

 
Reply With Quote
 
 
 
 
Alberto Poblacion
Guest
Posts: n/a
 
      16th Nov 2007
"Mike" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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.


 
Reply With Quote
 
Nicholas Paldino [.NET/C# MVP]
Guest
Posts: n/a
 
      16th Nov 2007
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]
- (E-Mail Removed)

"Alberto Poblacion" <earthling-(E-Mail Removed)> wrote
in message news:Ow$(E-Mail Removed)...
> "Mike" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>> 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.
>
>


 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Millisecond values missing when inserting datetime into datetime column of sql Server Manikandan Microsoft C# .NET 4 18th Jul 2007 08:59 PM
string comparasion =?Utf-8?B?U2FtIEhvZG8=?= Microsoft Excel Programming 1 21st Oct 2006 01:43 AM
How can I save a DateTime from my C# program into a SQL Server (datetime) database column. Steve Kershaw Microsoft ASP .NET 5 29th Aug 2006 03:10 AM
DateTime problem: how to create datetime format hh:mm:ss AM/PM without the date mimi Microsoft C# .NET 1 6th Aug 2004 08:28 PM
Re: Field Cocantination and Comparasion Allen Browne Microsoft Access Queries 1 13th May 2004 02:31 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 02:55 AM.