Problem with datetime

  • Thread starter Thread starter David
  • Start date Start date
D

David

Hi all,

I am using .NET 2

I have to work with short dates, e.g. 23 April 2008 will come to me as
23/04/08

I am splitting this value into int to put into a DateTime type...

DateTime MyDate = new DateTime(8, 4, 23);

However, when returned, it is returning as 23/04/0008 (obviously incorrect,
but the MyDate.WeekdayName is returning the correct week day name).

Later in my code, I have to do a select on a SQL based on the Date, and
23/04/2008 does not match 23/04/0008


Now, I can pass in the string direct (23/04/08) into my SQL and that will
work, however, I want to be ABSOLUTELY certain that say for example, 11 May
08 won't be interpreted as 5 Nov 08 which is why I am handling it by
splitting my incoming date string.

Any ideas on how I can get it to work the way I want?

--
Best regards,
Dave Colliver.
http://www.AshfieldFOCUS.com
~~
http://www.FOCUSPortals.com - Local franchises available
 
this might help:
//for date in english:

using System.Globalization;

CultureInfo dateSystem = new CultureInfo( "en-US" );


DateTime mydate = Convert.ToDateTime(this.txtDate.Text,dateSystem);

now mydate wich is from txtDate entered as :mm/dd/yy is good for SQL

also, in some point you may need to check the date before you pass it to
SQL:

String.Format("{0}/{1}/{2}", mydate.Month, mydate.Day, mydate.Year);

Vaf
 
David brought next idea :
Hi all,

I am using .NET 2

I have to work with short dates, e.g. 23 April 2008 will come to me as
23/04/08

I am splitting this value into int to put into a DateTime type...

DateTime MyDate = new DateTime(8, 4, 23);

Here you are specifying the year 8, instead of 2008. You have to handle
the conversion to "2008" (maybe by adding 2000, but what if you got
"99" meaning "1999"?)
However, when returned, it is returning as 23/04/0008 (obviously incorrect,
but the MyDate.WeekdayName is returning the correct week day name).

That may break for some other date.
Later in my code, I have to do a select on a SQL based on the Date, and
23/04/2008 does not match 23/04/0008


Now, I can pass in the string direct (23/04/08) into my SQL and that will
work,

SqlServer interprets two-digit years below 50 (I think) as 20xx and
higher values as 19xx. And it will break on years before 1752 or
thereabouts.
however, I want to be ABSOLUTELY certain that say for example, 11 May
08 won't be interpreted as 5 Nov 08 which is why I am handling it by
splitting my incoming date string.

Any ideas on how I can get it to work the way I want?

Do not pass the date as a string. Use a Parameter to pass the value to
sql and use the (correct) DateTime value directly as the parameter
value.


Hans Kesting
 
Hi,

Thanks for responding...

Inline...


Hans Kesting said:
David brought next idea :

Here you are specifying the year 8, instead of 2008. You have to handle
the conversion to "2008" (maybe by adding 2000, but what if you got "99"
meaning "1999"?)


I had considered this, but can't really hard code the 2000. Everything has
to be as flexible as it can. My Year actually comes in as 08, but the
DateTime requires it as Int, which changes 08 to 8.

That may break for some other date.


This is what I thought, so, this is why I need to ensure that the date
coming in is somehow understood as a full year.

SqlServer interprets two-digit years below 50 (I think) as 20xx and higher
values as 19xx. And it will break on years before 1752 or thereabouts.


I sort of worked that out when I passed in my date string.

Do not pass the date as a string. Use a Parameter to pass the value to sql
and use the (correct) DateTime value directly as the parameter value.


When I say passing to SQL, it is actually in a DataTable.Select() statement,
and I don't know how to pass this as a parameter.

I am loading all the dates locally, then when I need to check against a
date, do a DataTable.Select.
 
Back
Top