Powershell 两个相同的日期时间不相等

Powershell 两个相同的日期时间不相等,powershell,datetime,date-comparison,Powershell,Datetime,Date Comparison,据我所知,我有两个完全相同的日期 $theDate=(获取日期-小时0-分钟00-秒00) $otherDate=(获取日期-小时0-分钟00-秒00) 按该顺序执行 这两个参数都显示为2015年5月11日星期一上午12:00:00,但当我显示时($theDate-eq$otherDate)返回false。我试过$theDate.equals($otherDate)和($theDate)-eq($otherDate))同样的方法。 我唯一能返回true的是($theDate-gt$otherD

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

$theDate=(获取日期-小时0-分钟00-秒00)
$otherDate=(获取日期-小时0-分钟00-秒00)
按该顺序执行

这两个参数都显示为2015年5月11日星期一上午12:00:00,但当我显示时($theDate-eq$otherDate)返回false。我试过
$theDate.equals($otherDate)
($theDate)-eq($otherDate))
同样的方法。 我唯一能返回true的是
($theDate-gt$otherDate)

我是疯了还是只是个傻瓜?

你忘记了毫秒字段,这两个日期时间是不同的:

PS>$theDate=(获取日期-小时0-分钟00-秒00)
PS>$otherDate=(获取日期-小时0-分钟00-秒00)
PS>$theDate.毫秒
122
PS>$otherDate.毫秒
280
将这些字段设置为相同的值可以解决此问题:

PS>$theDate=(获取日期-小时0分钟00秒00毫秒000)
PS>$otherDate=(获取日期-小时0分钟00秒00毫秒000)
PS>$theDate-eq$otherDate
真的
虽然将两个变量分配给同一日期时间可能更容易:

PS>$theDate=(获取日期-小时0-分钟00-秒00)
PS>$otherDate=$theDate
PS>$theDate-eq$otherDate
真的

有趣的是,虽然
日期时间
精确到
勾号
(100ns),但设置
-00毫秒
也会将亚毫秒部分归零。或者,用户在格式化的
获取日期
上设置
获取日期(获取日期-f“MM/dd/yyyy”)
这将使您恰好在当天午夜到达。将其强制转换为
[datetime]
对象(如
[datetime](获取日期-f'MM/dd/yyyy')
)时也是如此。这么多选择@MadTechnician
(获取日期).Date.TimeOfDay
将是另一个选项。您可以做的一件事是
$theDate=(获取日期).Date
。它使用只返回日期部分和0'd out time部分的日期时间。只是一个提示-如果您想查看
日期时间
的“精确”值,请尝试检查其
Ticks
属性。