如果我使用02/16/16,我会收到错误消息。但是,如果我使用02/07/16它可以正常工作。
错误
C:\temp\.Net Exam Guide 2003\Exercises\Ch11\CDContent\Exercise 11-1>ChangeCultur
e.exe
Tuesday, February 16, 2016
Unhandled Exception: System.InvalidCastException: Conversion from string "02/16/
16" to type 'Date' is not valid.
at Microsoft.VisualBasic.CompilerServices.Conversions.ToDate(String Value)
at Module1.Main()
代码
进口系统
进口System.Globalization
进口System.Threading
Module Module1
Sub Main()
Dim DateString As String = "02/07/16"
Dim TestDate As DateTime = DateString
Console.WriteLine(TestDate.ToLongDateString)
Thread.CurrentThread.CurrentCulture = New CultureInfo("en-GB")
TestDate = DateString
Console.WriteLine(TestDate.ToLongDateString)
End Sub
End Module
预期结果
C:\temp\.Net Exam Guide 2003\Exercises\Ch11\CDContent\Exercise 11-1>ChangeCultur
e.exe
Sunday, February 07, 2016
02 July 2016
答案 0 :(得分:0)
必须删除将文化更改为en-GB后出现的以下行
TestDate = TestString