tags:

views:

406

answers:

3

I have been having problems working with dates. I need to have a DateTime instance that has the "dd-MM-yyyy" format. I'm NOT asking to have a string of my date instance in the "dd-MM-yyyy", that I know. I need to seed my date obj through the Entity framework, that calls a StoreProc that receives a param that it a Date... I can always change my SP to receive a Varchar instead of Date, but I want type safety.

The following code can help you to understand the problem:

Dim s1 As String = CurrentUICulture.ToString() 'pt-PT
Dim s2 As String = CurrentCulture.ToString()'pt-PT
Dim odate As Date = DateTime.ParseExact(sdate, CurrentUICulture.DateTimeFormat.ShortDatePattern, CurrentUICulture) 'sdate = 19/03/2009
'CurrentUICulture = pt-PT - ShortDatePattern = "dd-MM-yyyy"
'odate is 03/19/2009 !!!

Convert.ToDateTime(sdate, CurrentUICulture)
'its the same!

HELP!

+1  A: 

DateTimes don't have a format. Neither are they instances, since DateTime is a value type.

It seems to me you think the DateTime is 'incorrectly formatted' (you claim it is 03/19/2009). How did you check the value of odate? By printing it out as odate.ToString(), or checking its value in a debugger window? Try printing it out with:

Debug.Print odate.ToString(_
   Globalization.CultureInfo.CurrentUICulture.DateTimeFormat.ShortDatePattern, _
   Globalization.CultureInfo.CurrentUICulture)

odate is just a DateTime - it contains the number of seconds since some date long ago (which one I can't be bothered to remember right now). It has no formatting info, and so will be formatted on output.

The fact that the date is 03/19/2009 means that your parsing has succeeded. 03/19/2009 is just the 19th of march 2009 (the 3rd of the 19th month is not a valid date). So stop worrying and move on ;-)

Tor Haugen
"It seems to me you think the DateTime is 'incorrectly formatted' ("I don't think...I Know!I checked in debug mode.By the way:"DateTime Constructor (Int32, Int32, Int32, Int32, Int32, Int32, Calendar)Initializes a new instance of the DateTime structure
Txugo
to the specified year, month, day, hour, minute, and second for the specified calendar." Instance!And it has formatting info. And I need it so I dont have a Overflow exception back from sql srv.So, If your not smart enouth to answer nor to even undersant the question...don't botter to write.
Txugo
A: 

Whenever dealing with dates and SQL Server (which I assume you are here, too), I would recommend using the ISO-8601 format - it should work regardless of your language, culture or other settings.

ISO-8601 defines the date format to be: YYYY-MM-DD (with or without the dashes; YYYYMMDD is also acceptable).

Does this work?

Dim oDate as Date = DateTime.Parse("2009-03-06")

for a date of March 6, 2009.

Also see: http://www.w3.org/TR/NOTE-datetime

and: http://www.cl.cam.ac.uk/~mgk25/iso-time.html

Cheers Marc

marc_s
A: 

Problem Solved...because, there was no problem in the first place!

The thing is I was using a function that is something like this

GetFormatedDate(ByVal sdate As String) As Date

and...if the sdate is nothing (or null) I had a invalid date when it reached the Sql Store Proc...

ValueTypes don't have nothing has a value...but it's possible to do this:

GetFormatedDate(ByVal sdate As String) As Global.System.Nullable(Of Date)

with that I no longer received a Invalid param exception.

yeah!

Txugo