I'm writing a VB.Net windows forms application. This line of code:
Personal.Effect iveDate = GridRow2.Cells( "New
Value").Value.T oString.Trim
Fails with this error:
Cast from string "8/23/2005" to type 'Date' is not valid.
The date being returned is in US English and my system is set for
Canadian English. This is what I don't get. If my regional settings
are set for Canadian English, why is VB.Net returning US English
formatted dates?
? GridRow2.Cells( "New Value").Value.T oString.Trim
Result: "8/23/2005"
? Personal.Effect iveDate
Result "#8/23/2005#"
Why? My system is set for Canadian English (dd/mm/yyyy), why is VB
returning US English dates (mm/dd/yyyy)? Someone suggested this:
Threading.Threa d.CurrentThread .CurrentCulture = New
System.Globaliz ation.CultureIn fo("en-CA")
Threading.Threa d.CurrentThread .CurrentUICultu re = New
System.Globaliz ation.CultureIn fo("en-CA")
I put this in the Form_Load of my startup form. It makes no
difference. Then this was suggested:
Dim s As String = "8/25/2005"
Dim dt As Date = DateTime.Parse( s, New
System.Globaliz ation.CultureIn fo("en-US"))
That works. But I still don't get it. Above, we're parsing the date
assuming it's in US English. What if the system is set for UK
English? It'll return a UK date and crash with an invalid date error
again. Another problem is I can't do a "IsDate(MyD ate) if the
regional settings are in conflict. Although the DateTime.Parse will
return a properly parsed date, the test for it will return false.
I don't understand:
- Why is VB returning US English dates when my regional settings are
set to CA English?
- How does an app like Excel or Outlook not crash at every turn if I
change the regional settings?
I'm confused, any advice appreciated.