gpt4 book ai didi

powershell - 如何在 PowerShell 中通过::parseexact 解析日期时间?

转载 作者:行者123 更新时间:2023-12-04 15:17:47 26 4
gpt4 key购买 nike

我在尝试解析时遇到问题,并使用 ::parseexact 在 PowerShell 中将字符串转换为 [DateTime] 格式| .谁能告诉我我哪里不好?
这是我的代码。

[datetime]::parseexact('2018-05-07T15:19:17.839+03:00','o', 'yyyy-MM-ddTHH:mm:ss.fffzzz')

这是一个错误

Cannot find an overload for "parseexact" and the argument count: "3". At line:1 char:1 + [datetime]::parseexact('2018-05-07T15:19:17.839+03:00','o', 'yyyy-MM- ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (:) [], MethodException + FullyQualifiedErrorId : MethodCountCouldNotFindBest

最佳答案

如评论中所述,您可以将代码更改为 [datetime]::parseexact('2018-05-07T15:19:17.839+03:00', 'yyyy-MM-ddTHH:mm:ss.fffzzz',$null)如果您不关心 CultureInfo ,这对您有用.

但是正如我在您的代码中看到的那样,您为 ParseExact 提供了 3 个参数。函数,这让我怀疑你是否真的想改变 CultureInfo .如果你想改变你的那个,你可以做这样的事情 -

([datetime]::ParseExact($date,"dd/MM/yyyy",[Globalization.CultureInfo]::CreateSpecificCulture('en-GB'))


([datetime]::ParseExact($date,"dd/MM/yyyy",[Globalization.CultureInfo]::CreateSpecificCulture('de-DE'))

等等,取决于您的要求。

额外信息 -

根据 the msdn article ,

The DateTime.ParseExact(String, String[], IFormatProvider, DateTimeStyles) method parses the string representation of a date that matches any one of the patterns assigned to the formats parameter. If the string s does not match any one of these patterns with any of the variations defined by the styles parameter, the method throws a FormatException. Aside from comparing s to multiple formatting patterns, rather than to a single formatting pattern, this overload behaves identically to the DateTime.ParseExact(String, String, IFormatProvider, DateTimeStyles) method.


style参数,是枚举值的按位组合,指示 s(输入日期)的允许格式。要指定的典型值是 None .

Get-Date Formatting Culture了解更多信息。

关于powershell - 如何在 PowerShell 中通过::parseexact 解析日期时间?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50214955/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com