Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / ASP.NET / General / April 2008

Tip: Looking for answers? Try searching our database.

Problem with datetime

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
David - 23 Apr 2008 16:09 GMT
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?

Signature

Best regards,
Dave Colliver.
http://www.AshfieldFOCUS.com
~~
http://www.FOCUSPortals.com - Local franchises available

fcs - 23 Apr 2008 16:36 GMT
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

> Hi all,
>
[quoted text clipped - 20 lines]
>
> Any ideas on how I can get it to work the way I want?
Hans Kesting - 23 Apr 2008 16:39 GMT
David brought next idea :
> Hi all,
>
[quoted text clipped - 6 lines]
>
> 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
David - 23 Apr 2008 17:10 GMT
Hi,

Thanks for responding...

Inline...

> David brought next idea :
>> Hi all,
[quoted text clipped - 11 lines]
> 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.

>> 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.

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.

>> 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
[quoted text clipped - 4 lines]
> 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.

>> 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
[quoted text clipped - 4 lines]
> 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.

> Hans Kesting

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.