【问题标题】:What am I not getting about [DateTime]::ParseExact()?关于 [DateTime]::ParseExact(),我没有得到什么?
【发布时间】:2017-07-19 23:00:20
【问题描述】:
$timeinfo = "01-‎06‎-2017 ‏‎12:34"
$template = "dd-MM-yyyy HH:mm"
[DateTime]::ParseExact($timeinfo, $template, $null)

结果:

使用“3”参数调用“ParseExact”的异常:“无法识别字符串 作为有效的日期时间。” 在行:3 字符:1 + [日期时间]::ParseExact($timeinfo, $template, $null) + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~ + CategoryInfo : NotSpecified: (:) [], MethodInvocationException + FullyQualifiedErrorId : 格式异常

我不知道这里出了什么问题?当我在模板中指定它应该如何读取它时,为什么我的字符串不是有效的日期时间?

【问题讨论】:

    标签: powershell datetime exception format


    【解决方案1】:

    $timeinfo 变量中有一些奇怪的字符。复制和粘贴时,我得到这个:

    您可以通过按向右或向左箭头并遍历字符串来看到这一点;它停在奇怪的字符处。

    更改为$timeinfo = "01-06-2017 12:34",您的代码按预期工作。复制并粘贴此字符串进行测试。

    编辑 - 使用Unicode converter,看起来这是LRM control character

    【讨论】:

    • @user3272783 没问题 - 这让我之前在证书指纹中遇到过。如果您有兴趣,请添加更多信息以回答。考虑accepting/upvoting的答案,如果你觉得它有帮助!
    • 你如何使用脚本中的函数从字符串中删除这些奇怪的字符?
    【解决方案2】:

    您可能复制并粘贴代码,因为$timedate 中的字符不正确,请尝试复制并粘贴此代码:

    $timeinfo = "01-06-2017 12:34"
    $template = "dd-MM-yyyy HH:mm"
    [DateTime]::ParseExact($timeinfo, $template, $null)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-19
      • 2013-07-15
      • 1970-01-01
      相关资源
      最近更新 更多