日期时间减法在 PowerShell 中不起作用 - 赋值与相等运算符 [英] DateTime subtraction not working in PowerShell - assignment vs. equality operator

查看:25
本文介绍了日期时间减法在 PowerShell 中不起作用 - 赋值与相等运算符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

今天 (2017-05-29) 我在 Windows 7 Enterprise 上使用 PowerShell 5.0.10586.117 并运行以下(缩短):

Today (2017-05-29) I am using PowerShell 5.0.10586.117 on Windows 7 Enterprise and run the following (shortened):

$dateOfLicense = "2017-04-20"
$dateOfToday = '{0:yyyy-MM-dd}' -f (Get-Date)

$TimeDifference = [DateTime]$dateOfToday - [DateTime]$dateOfLicense 
if (($TimeDifference) = 14)
{
    Write-Host "test"
}

即使这两天相差 39 天,我的代码也会跳转到 if 子句并将测试"发送到屏幕.

Even the difference between both days is 39, my code jumps in the if-clause and sends "test" to screen.

我在这里做错了什么?

推荐答案

您正在分配 14$TimeDifference.相反,您不想使用 -le 比较 Days 属性:

You are assigning 14 to $TimeDifference. Instead you wan't to compare the Days property using -le:

if ($TimeDifference.Days -le 14)
{
    Write-Host "test"
}

这篇关于日期时间减法在 PowerShell 中不起作用 - 赋值与相等运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆