【发布时间】:2020-12-23 20:36:58
【问题描述】:
当我尝试解析 IIS 日志文件中的一行并将日期时间拉到该行时,我收到以下错误
使用“3”个参数调用“ParseExact”的异常:“字符串不是 识别为有效的日期时间。”在 line:9 char:1
- $dateTime = [datetime]::ParseExact($dateTimeString, 'yyyy-MM-dd HH:mm ...
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException + FullyQualifiedErrorId : FormatException
这是我的 powershell 代码:
$line = '2020-12-23 02:01:16.244 -06:00 [Information] 2020-12-23T08:01:16.2446161Z: [Error] Oracle.ManagedDataAccess.Client.OracleException (0x80004005): Connection request timed out'
$dateTimeString = [regex]::Matches($line, '\d\d\d\d-\d\d-\d\d\s\d\d:\d\d:\d\d')[0].Groups[1].Value
write-host "datetimestr:" $dateTimeString
$provider = New-Object System.Globalization.CultureInfo "en-US"
$dateTime = [datetime]::ParseExact($dateTimeString, 'yyyy-MM-dd HH:mm:ss', $provider)
write-host $dateTime
$dateTime -f 'MM/dd/yyyy HH:mm:ss' | write-host
我想我需要一些新鲜的眼睛来看看我错过了什么。
注释:
- 我已尝试复制并粘贴 $line var 中的字符串以验证日期时间中没有奇怪的字符
- 我尝试将 .trim 添加到 $line 中,但没有帮助
- 我还将正则表达式模式缩小到 '\d\d\d-\d\d-\d\d' 和 parseexact() 到 ($dateTimeString, 'yyyy-MM-dd', $provider)这也无济于事。
【问题讨论】:
标签: regex powershell datetime