Datetime issue

UPDATE: Thanks for the comments, it gave me some directions. Well It's working now with this little test:

If CultureInfo.CurrentCulture.Name.IndexOf("US") > -1 Then

dtEndRange = Now.ToString("MM/d/yy")

Else

dtEndRange = Now.ToString("d/MM/yy")

End If

And I am not certain after all this was coming from SQL Server, more from the Windows server settings.

 

I have 2 SQL installations on two different machines. One is installed with dates in US and the other in European format.

In my code I need to send to a stored procedure the actual date.

Well the problem here is that I have an error if I send the European formatted date to the US box and vice versa for the other box.

The last thing I tried is this but it works obviopusly only on one box:

dtEndRange = Now.ToString("MM/d/yy")

I heard somewhere, and I can't remember how to use it, something about a Culture class.

Any idea ?

 

7 Comments

  • System.Globalization



    CultureInfo?

  • Yeah, you essentially want to do:



    myDateTime.ToString(new CultureInfo("en-US"));

  • Thanks Drew I was just on the edge to have this but instead I am playing also with this



    Dim MyCult as datetimeformatinfo = new CultureInfo("en-US", False).DateTimeFormat



    Dim dtEndRange as new datetime(Now.ToString(MyCult.ShortDatePattern))



    What do you think ?

  • You should be able to

    .ToString(CultureInfo.CreateSpecificCulture("en-US"))

  • Thanks Joe, but the solution I found is working now ;-)

  • Maybe working now, but if you don't get it right it'll come back and bite you!

    Try accessing the SQL Server on box A from an app on box B and vice versa.

  • Best is to use universal datetime format for SQL called "ODBC Datetime format" (see mk:@MSITStore:C:\Program%20Files\Microsoft%20SQL%20Server\80\Tools\Books\acdata.chm::/ac_8_con_03_04l0.htm) in SQL Server Books Online.

    This format of date is suppport in all configuration of SQL 2000.



    You can use this function for converting DateTime value to ODBC Datetime format:



    Shared Function PrepareValueForSqlQuery(ByVal val As Date) As String

    '{ ts '1998-05-02 01:23:56.123' }



    Dim sDat As String



    sDat = "{ ts '" & Year(val) & "-" & AddZero(Month(val)) & "-" & AddZero(Day(val)) & " " & AddZero(Hour(val)) & ":" & AddZero(Minute(val)) & ":" & AddZero(Second(val)) & "." & AddZero(val.Millisecond, 3) & "' }"



    Return sDat

    End Function





    Private Shared Function AddZero(ByVal str As String, Optional ByVal DestLength As Integer = 2) As String

    Return str.PadLeft(DestLength, "0")

    End Function

Comments have been disabled for this content.