首页 > 解决方案 > vb.net 应用程序可以在一台机器上正确转换日期,但不能在另一台机器上正确转换 - GB/US 格式

问题描述

我编写了一个应用程序,它从我们的 HR 系统中导出 CSV,循环遍历所有记录并将 HR 系统中的值应用到活动目录。

它很有效,在我的机器上运行时,我没有任何错误。

当在我们的一台服务器上运行它时,它最终将运行并由服务帐户执行,我收到日期转换错误......

 System.InvalidCastException: Conversion from string "21/08/2020" to type 'Date' is not valid.

在我的代码开始时,我正在定义区域......

    Dim ukCulture = New Globalization.CultureInfo("en-GB")
    System.Threading.Thread.CurrentThread.CurrentCulture = ukCulture

如果我在运行时查询当前文化,它会显示“en-GB”,所以这似乎是正确的。如果我写出日期字符串,它们看起来都正确,并且比较操作工作正常。

该错误似乎发生在这部分代码中......

 Dim converted_hr_accountexpiry_timestamp= hr_row(0).Item("Termination Date") & ""
 Dim hr_termdate_var() As String = converted_hr_accountexpiry_timestamp.split("/")
 updatescript = updatescript.Replace("$x", "'" & hr_termdate_var(0) & "'") _
                    .Replace("$y", "'" & hr_termdate_var(1) & "'") _
                    .Replace("$z", "'" & hr_termdate_var(2) & "'")

因此,对于上下文,此代码正在构建一个 powershell 脚本,执行该脚本以在 AD 中进行必要的更改。我们在这里看到的那个powershell代码部分是这个......

 $server = "MyPrimaryDNSServer.FQDN"
 $exp = get-date -Day $x -Month $y -Year $z -Hour 00 -Minute 00 -Second 00
 $expirydate = $exp.ToUniversalTime().AddDays(1)

很明显,它试图使用美国日期格式,因为如果提供的日期与可接受的美国日期匹配,即 2020 年 3 月 5 日,那么它将接受它并应用错误的日期。仅当日期的日期 (dd) 部分在美国格式日期(即 2020 年 7 月 31 日)上不被接受为 MM 时,才会引发该错误。

并重新迭代;这个问题不会发生在我的机器上,只会发生在最终将执行应用程序的服务器上。我已经完成了该设备本身的所有区域设置,并且所有内容都设置为英国,日期的 dd/MM/yyyy 格式正确。我对这个完全不知所措,拔掉了我剩下的一点头发。

任何建议/帮助appriciated!

编辑1:

这是完整的例外,减去用户名 obvs ...

 Error with account : Joe Bloggs (1010245)
 System.InvalidCastException: Conversion from string "24/07/2020" to type 'Date' is not valid.
 at Microsoft.VisualBasic.CompilerServices.Conversions.ToDate(String Value)
 at Microsoft.VisualBasic.CompilerServices.Operators.CompareObject2(Object Left, Object Right, Boolean TextCompare)
 at Microsoft.VisualBasic.CompilerServices.Operators.CompareObjectEqual(Object Left, Object Right, Boolean TextCompare)
 at Atlas.Main.GetAccountsWithUpdates()

编辑2:

所以看起来发生了两个错误,这就是为什么我无法通过依次注释每个相关行来找到它。

这绝对是错误线之一...

 converted_ad_expiry_timestamp = converted_ad_expiry_timestamp.ToString("dd/MM/yyyy").Split(" ")(0)

返回的值是日期时间而不是日期,因此我使用 tostring 并将其拆分在空间上以仅获取日期部分。

第二个错误似乎发生在这里......

 If Not (converted_hr_expiry_timestamp = converted_ad_expiry_timestamp) Then updateme = True : If (hr_row(0).Item("Termination Date")) = "" Then account_expiration_date = "$null" Else account_expiration_date = converted_hr_expiry_timestamp

标签: vb.netvisual-studio-2019date-conversion

解决方案


我知道这只是解决问题,并将停止它在本地为您工作(或者更确切地说将问题转移到您的机器上),但如果它只在该服务器上运行,您不仅可以交换 X 和 Y 值?

Dim converted_hr_accountexpiry_timestamp= hr_row(0).Item("Termination Date") & ""
Dim hr_termdate_var() As String = converted_hr_accountexpiry_timestamp.split("/")
updatescript = updatescript.Replace("$x", "'" & hr_termdate_var(1) & "'") _
                .Replace("$y", "'" & hr_termdate_var(0) & "'") _
                .Replace("$z", "'" & hr_termdate_var(2) & "'")

或者,如果您希望它在两个系统上都可以工作,那么在运行有问题的代码之前可能需要进行检查,这样的事情过去对我有用:

Try
    Dim TempTimeString As String = "31/01/2020 01:00 AM"
    Dim ConvertedTime As Date
    ConvertedTime = DateTime.Parse(TempTimeString)
    '''Don't swap X and Y as it was able to convert
Catch ex As Exception
    '''swap X and Y as it was unable to convert
End Try 

推荐阅读