$tmpCreated = dir | Sort CreationTime -Descending | Select CreationTime -First 1
$tmpCreated = [string]$tmpCreated
$tmpCreated = $tmpCreated.split("=")[1]
$tmpCreated = $tmpCreated.split("}")[0]
这是我想要作为DateTime得到的变量。我想用这个命令比较两个日期:
$timeDiff = new-timespan –Start $tmpLast –End $tmpCreated
$tmpLast
运行正常。但是当我尝试启动时出现错误,说-End参数无法使用,因为$tmpCreated
转换失败。但这里是它包含的字符串:11/30/2015 11:57:01
。那么,有人知道这有什么问题吗?
答案 0 :(得分:5)
CreationTime
属性已包含DateTime
对象,因此您只需展开属性:
$tmpCreated = Get-ChildItem |
Sort-Object CreationTime -Descending |
Select-Object -Expand CreationTime -First 1
$timeDiff = New-TimeSpan –Start $tmpLast –End $tmpCreated
答案 1 :(得分:2)
您无需将日期转换为字符串即可将其与New-TimeSpan
$tmpCreated = dir | Sort CreationTime -Descending | Select CreationTime -First 1
$timeDiff = new-timespan –Start $tmpLast –End $tmpCreated.CreationTime
会好起来的。