据我所知,我有两个相同的日期。
$theDate = (Get-Date -Hour 0 -Minute 00 -Second 00)
$otherDate = (Get-Date -Hour 0 -Minute 00 -Second 00)
按那个顺序执行
这两个都显示为
Monday, May 11, 2015 12:00:00 AM
,但是当我这样做时 ($theDate -eq $otherDate)
它返回假。我试过 $theDate.equals($otherDate)
和 (($theDate) -eq ($otherDate))
一样。我唯一能返回 true 的是
($theDate -gt $otherDate)
我是疯了还是只是个菜鸟?
最佳答案
您忘记了毫秒字段,这对于两个日期时间会有所不同:
PS > $theDate = (Get-Date -Hour 0 -Minute 00 -Second 00)
PS > $otherDate = (Get-Date -Hour 0 -Minute 00 -Second 00)
PS > $theDate.Millisecond
122
PS > $otherDate.Millisecond
280
将这些字段设置为相同的值可以解决问题:
PS > $theDate = (Get-Date -Hour 0 -Minute 00 -Second 00 -Millisecond 000)
PS > $otherDate = (Get-Date -Hour 0 -Minute 00 -Second 00 -Millisecond 000)
PS > $theDate -eq $otherDate
True
尽管将两个变量分配给相同的日期时间可能更容易:
PS > $theDate = (Get-Date -Hour 0 -Minute 00 -Second 00)
PS > $otherDate = $theDate
PS > $theDate -eq $otherDate
True
关于powershell - 两个相同的日期时间不相等,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30177278/