【问题标题】:Error: "String was not recognized as a valid DateTime."错误:“字符串未被识别为有效的日期时间。”
【发布时间】:2020-05-06 21:58:00
【问题描述】:

我在使用 Get-Date 命令时遇到了一些问题,并且出现错误:

 Get-Date : Cannot bind parameter 'Date'. Cannot convert value "29/01/2020 00:00:00" to type "System.DateTime". Error: "String was not recognized as a valid DateTime."

我的部分代码:

$CEST = 7200
        $CSV = Import-Csv -Path "D:\1.csv"  -Delimiter ';' | Select-Object -SkipLast 2
        $CSV | ForEach-Object {

         $_.'Scheduled Start Date' = $CEST + [Math]::Floor([decimal](Get-Date(Get-Date $_.'Scheduled Start Date').ToUniversalTime()-uformat "%s"))
       $_.'Scheduled End Date' = $CEST + [Math]::Floor([decimal](Get-Date(Get-Date $_.'Scheduled End Date').ToUniversalTime()-uformat "%s")) 

           $_.'Actual Start Date' = 
                If([string]::IsNullOrEmpty($_.'Actual Start Date')) {
                      $_.'Actual Start Date' = ""
            }

                 ElseIf(![string]::IsNullOrEmpty($_.'Actual Start Date')) {  

                 $CEST + [Math]::Floor([decimal](Get-Date(Get-Date $_.'Actual Start Date').ToUniversalTime()-uformat "%s"))
                   $_.'Scheduled Start Date' = $CEST + [Math]::Floor([decimal](Get-Date(Get-Date $_.'Actual Start Date').ToUniversalTime()-uformat "%s"))    
                       }


           $_.'Actual End Date' = 
                If([string]::IsNullOrEmpty($_.'Actual End Date')) {
                      $_.'Actual End Date' = ""
            }

                 ElseIf(![string]::IsNullOrEmpty($_.'Actual End Date')) {  

                 $CEST + [Math]::Floor([decimal](Get-Date(Get-Date $_.'Actual End Date').ToUniversalTime()-uformat "%s"))
                 $_.'Scheduled End Date' = $CEST + [Math]::Floor([decimal](Get-Date(Get-Date $_.'Actual End Date').ToUniversalTime()-uformat "%s"))

                       }  


} 
$CSV | 
Export-Csv -Path "D:\1_tmp.csv" -Delimiter ';' -NoTypeInformation

日期以如下格式保存在 1.csv 文件中的多行列中(计划结束日期、计划开始日期、实际结束日期、实际开始日期)

27/05/2020 07:00:00

当我在计算机上运行 Get-Date 命令时,输出为:

Wednesday, May 6, 2020 2:59:59 PM

请支持!

【问题讨论】:

  • 您可以尝试使用[datetime]::ParseExact($_.'Actual Start Date', 'dd/MM/yyyy HH:mm:ss', $null)解析CSV文件中给出的日期
  • 我添加了,CEST = 7200 $CSV = Import-Csv -Path "D:\1.csv" -Delimiter ';' | Select-Object -SkipLast 2 [datetime]::ParseExact($_.'Actual Start Date', 'dd/MM/yyyy HH:mm:ss', $null) [datetime]::ParseExact($_.'Actual End Date', 'dd/MM/yyyy HH:mm:ss', $null) [datetime]::ParseExact($_.'Scheduled End Date', 'dd/MM/yyyy HH:mm:ss', $null) [datetime]::ParseExact($_.'Scheduled Start Date', 'dd/MM/yyyy HH:mm:ss', $null) $CSV | ForEach-Object { ......但它不起作用

标签: powershell


【解决方案1】:

查看您的代码,您希望将格式为 dd/MM/yyyy HH:mm:ss 的 CSV 中的日期字符串转换为带有 $CEST 时区值的 Unix 时间戳。

我建议不要重复相同的长结构,而是使用一个小的辅助函数来做到这一点:

function Get-TimeStamp ([string]$dateString, [int]$offset = 0) {
    if (![string]::IsNullOrWhiteSpace($dateString)) {
        $epoch = [DateTime]::new(1970, 1, 1, 0, 0, 0, 0, 'Utc')
        $date  = Get-Date ([datetime]::ParseExact($dateString, 'dd/MM/yyyy HH:mm:ss', $null))
        $diff  = (Get-Date $date).ToUniversalTime() - $epoch
        return [int64]([Math]::Floor($diff.TotalSeconds) + $offset)

        # OR use the -UFormat way like
        # $date = Get-Date ([datetime]::ParseExact($dateString, 'dd/MM/yyyy HH:mm:ss', $null)).toUniversalTime() -UFormat '%s'
        # return [int64]$date + $offset
    }
}

在该函数下方,代码如下所示:

$CEST = 7200
Import-Csv -Path "D:\1.csv"  -Delimiter ';' | Select-Object -SkipLast 2 | ForEach-Object {
    [PsCustomObject] @{
        'Scheduled Start Date' = Get-TimeStamp $_.'Scheduled Start Date' $CEST
        'Scheduled End Date' = Get-TimeStamp $_.'Scheduled End Date' $CEST
        'Actual Start Date' = Get-TimeStamp $_.'Actual Start Date' $CEST
        'Actual End Date' = Get-TimeStamp $_.'Actual End Date' $CEST
    }
} | Export-Csv -Path "D:\1_tmp.csv" -Delimiter ';' -NoTypeInformation

【讨论】:

    猜你喜欢
    • 2012-01-07
    • 2023-04-01
    • 1970-01-01
    • 2011-12-15
    • 1970-01-01
    • 1970-01-01
    • 2014-06-09
    • 2012-04-24
    相关资源
    最近更新 更多