Vb.net 从字符串转换的日期无效
如下所示,我在尝试从字符串转换日期时看到以下错误。奇怪的是,相同的代码在另一台配置了相同区域的服务器上正常工作。此外,如果我从控制台应用程序运行这段代码,它就会工作。此错误仅在windows服务中计划时发生。为什么会这样,有更好的方法吗 带有调试信息的代码:Vb.net 从字符串转换的日期无效,vb.net,Vb.net,如下所示,我在尝试从字符串转换日期时看到以下错误。奇怪的是,相同的代码在另一台配置了相同区域的服务器上正常工作。此外,如果我从控制台应用程序运行这段代码,它就会工作。此错误仅在windows服务中计划时发生。为什么会这样,有更好的方法吗 带有调试信息的代码: 在使用之前,您尝试使用CDate将字符串值转换为日期 因此,只有在有效的情况下,您才能使用以下代码设置日期: 如果fileDateStr始终采用相同的格式(在您的情况下MM/dd/yyyy-),也可以使用: 如果fileDateStr有多
在使用之前,您尝试使用
CDate
将字符串值转换为日期
因此,只有在有效的情况下,您才能使用以下代码设置日期:
如果fileDateStr
始终采用相同的格式(在您的情况下MM/dd/yyyy
-),也可以使用:
如果fileDateStr有多种有效格式,您也可以使用:
正如Sebastian Brosch的回答中所述,在将字符串解析为日期之前进行转换
作为服务运行时无法转换的原因可能是运行服务的用户的不同区域性/区域设置。
根据这些设置,“2017年11月16日”将不会用TryParse
解析
如果fileDateStr
的格式始终为“MM/dd/yyyy”,则可以使用
相反
请将您的代码复制并粘贴为文本,不要将其包含为图片。代码在TryParse
点上似乎不清楚:您试图将Date
解析为Date
,并且由于您无条件地使用CDate
,因此得到了TryParse
旨在避免的异常。您应该消除TryParse
并让尝试处理错误,或者消除CDate
调用并解决TryParse
调用的False
分支中的任何日期错误。谢谢!但是,您知道为什么它在一台服务器上工作,并在另一台服务器上给出错误(仅在windows服务中),因为即使在第二台服务器上使用console应用程序也在第一台服务器上工作:O。对我来说,这应该是每一次错误。欢迎您!我不知道为什么它不起作用。这可能是全球化或子字符串的无效结果。还有一件事我检查得更深入了,而且自从我实现了您的修复后,我再也没有收到错误,这一行:Date.TryParse(fileDateStr,dateValue)为true。。。你能帮我吗?所以Date.TryParse
无法从字符串中获取日期值。您可以使用带格式字符串的TryParseExact
尝试解决方案。还要确保fileDateStr
上的值是有效的日期(采用预期格式)。您还可以检查并设置应用程序的应用程序。区域性
,以确保使用了正确的区域性。我进行了更深入的检查,除非Sebastian answer not giving error,否则不会对其进行分析。。。这行的意思是:Date.TryParse(fileDateStr,dateValue)不是真的。你能帮我修一下吗?请注意,在另一台服务器上,即使在我进行修复之前,一切都正常工作,因此我希望在迁移到该服务器时,一切都正常工作,不是吗;有没有通用的方法来解决这个问题?我不明白。什么不清楚?我的回答正好解决了“11/16/2016”无法用TryParse
解析的问题,这取决于区域设置,因此将返回False
。因此,您应该使用TryParseExact
。
Try
Dim fileDateStr = Data(0, 0).ToString()
fileDateStr = fileDateStr.Substring(0, fileDateStr.IndexOf("(", StringComparison.Ordinal))
Dim dateValue As Date
If Date.TryParse(CDate(fileDateStr), dateValue) Then
ReportDate = CDate(fileDateStr)
End If
Catch ex As Exception
..
Dim fileDateStr = Data(0, 0).ToString()
fileDateStr = fileDateStr.Substring(0, fileDateStr.IndexOf("(", StringComparison.Ordinal))
Dim dateValue As Date
If Date.TryParse(fileDateStr, dateValue) Then
ReportDate = dateValue
End If
Dim fileDateStr = Data(0, 0).ToString()
fileDateStr = fileDateStr.Substring(0, fileDateStr.IndexOf("(", StringComparison.Ordinal))
Dim dateValue As Date
If Date.TryParseExact(fileDateStr, "MM/dd/yyyy", CultureInfo.InvariantCulture, Globalization.DateTimeStyles.None, dateValue) Then
ReportDate = dateValue
End If
Dim fileDateStr = Data(0, 0).ToString()
fileDateStr = fileDateStr.Substring(0, fileDateStr.IndexOf("(", StringComparison.Ordinal))
Dim dateValue As Date
Dim formats As String() = {"MM/dd/yyyy", "yyyy-MM-dd"}
If Date.TryParseExact(fileDateStr, formats, CultureInfo.InvariantCulture, Globalization.DateTimeStyles.None, dateValue) Then
ReportDate = dateValue
End If
Dim fileDateStr = Data(0, 0).ToString()
fileDateStr = fileDateStr.Substring(0, fileDateStr.IndexOf("(", StringComparison.Ordinal))
Dim dateValue As Date
If Date.TryParseExact(fileDateStr,
"MM/dd/yyyy",
CultureInfo.InvariantCulture,
DateTimeStyles.None,
dateValue) Then
ReportDate = dateValue
End If