在PowerShell中将字符串转换为24小时时间格式

jja*_*son 4 string powershell time

我将一些字符串短时间值转换为24小时时间格式时遇到问题.

在脚本中,我有以下foreach循环来解析来自a的每一行数据csv:

Foreach ($Line in $Data) {
    $command.CommandText=$SQLCommandPre+ `
        "'"+$line."Reference" `
        +"','"+$line."Date" `
        +"','"$line."Business Hour".SubString(0,8) `
        +"','"+"{0:f2}" -f ($line."Total"/2) `
    +"','"+"{0:f0}" -f ($line."Number"/2) `
    +"')"
    [void] $command.ExecuteNonQuery()
}
Run Code Online (Sandbox Code Playgroud)

例如,我的源数据是:

0101,8/19/2012,12:00 AM - 12:59 AM,241.83,21
Run Code Online (Sandbox Code Playgroud)

目前在脚本中它只是抓住12点,但我需要转换为00:00.我尝试使用以下内容,但它对查询powershell创建没有任何影响:

"{0:HH:mm}" -f ($line."Business Hour".SubString(0,8))
Run Code Online (Sandbox Code Playgroud)

我一直无法找到另一种方法来达到我的目标.

Kei*_*ill 7

在尝试格式化之前,请确保将检索到的字符串转换为[DateTime]对象,例如:

C:\PS> "{0:HH:mm}" -f [datetime]'12:00 AM'
00:00
Run Code Online (Sandbox Code Playgroud)