为什么CDate和parseExact将今天的日期(10月3日)(表示为03/10/2012)转为10/03/2012?我使用的是Windows 7,VS2012。控制面板中的所有设置均为UK / GB。我试过添加行
System.Threading.Thread.CurrentThread.CurrentCulture = New System.Globalization.CultureInfo("en-GB", True)
System.Threading.Thread.CurrentThread.CurrentUICulture = New System.Globalization.CultureInfo("en-GB", True)
没有效果。
这是我的代码 - 您将看到我必须交换文本版本的日期和月份才能获得我想要的日期
dim txtDate As String = "03/10/2012"
Dim strOriginalDate As String = txtDate ' 03/10/2012
Dim dtmdate1 As Date = CDate(txtDate) ' #10/03/2012#
Dim dtmdate2 As Date = DateTime.ParseExact(txtDate, "dd/MM/yyyy", Nothing) ' #10/03/2012#
txtDate = Split(txtDate, "/")(1) & "/" & Split(txtDate, "/")(0) & "/" & Split(txtDate, "/")(2)
Dim dtmdate3 As Date = CDate(txtDate) ' #3/10/2012#
答案 0 :(得分:1)
我怀疑您正在查看调试器的“Locals”窗格中的日期值。如果您实际写出了值,您会发现它们符合要求:
Module Module1
Sub Main()
System.Threading.Thread.CurrentThread.CurrentCulture = New System.Globalization.CultureInfo("en-GB", True)
System.Threading.Thread.CurrentThread.CurrentUICulture = New System.Globalization.CultureInfo("en-GB", True)
Dim txtDate As String = "03/10/2012"
Dim strOriginalDate As String = txtDate
Dim dtmdate1 As Date = CDate(txtDate)
Console.WriteLine(dtmdate1.ToString("dd MMM yyyy"))
Dim dtmdate2 As Date = DateTime.ParseExact(txtDate, "dd/MM/yyyy", Nothing) ' #10/03/2012#
Console.WriteLine(dtmdate2.ToString("dd MMM yyyy"))
txtDate = Split(txtDate, "/")(1) & "/" & Split(txtDate, "/")(0) & "/" & Split(txtDate, "/")(2)
Dim dtmdate3 As Date = CDate(txtDate)
Console.WriteLine(dtmdate3.ToString("dd MMM yyyy"))
Console.ReadLine()
End Sub
End Module
输出的最后一行当然不是那么理想,因为你已经交换了月和日。
在W7 x64上使用VS2012 RC进行测试,设置为en-GB。您可能希望使用设置VS的区域设置进行测试:http://msdn.microsoft.com/en-US/library/9cytz106%28v=vs.80%29.aspx
将日期显示为日期文字与数据文字的定义方式完全一致,例如,请参阅Date Data Type (Visual Basic)的格式要求小节