两个相同的日期时间不比较相等

Two identical datetimes not comparing equal

据我所知,我有两个相同的日期。

$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) 它 return 是错误的。我试过 $theDate.equals($otherDate)(($theDate) -eq ($otherDate)) 相同的东西。 我唯一能做到 return 正确的是 ($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