简体   繁体   中英

Powershell date Comparison not working in if statement

I have a requirement to compare dates using the powershell Get-Date commandlet. An example of how I am using the Get-Date command is as follows.

$Date1 = Get-Date -Format "dd/MM/yyyy HH:mm:ss" -Day 31 -Hour 07 -Minute 30 -Month 12 -Second 00 -Year 2021

If $Date1 above was compared to the following $Date2 below using a simple

if ($Date1 -lt $Date2)
{
    Write-Host Date1 is before Date2
}

then Write-Host would be excuted. This is expected behaviour.

$Date2 = Get-Date -Format "dd/MM/yyyy HH:mm:ss" -Day 31 -Hour 08 -Minute 30 -Month 12 -Second 00 -Year 2021

If $Date2 was changed to the following:

$Date2 = Get-Date -Format "dd/MM/yyyy HH:mm:ss" -Day 01 -Hour 08 -Minute 30 -Month 1 -Second 00 -Year 2022  

The if statement would not excute Write-Host even though the $Date2 variable is still after $Date1 .

If you want to compare the dates, remove Format from your Get-Date . When you use Format you get string instead of DateTime object:

PS> $Date1 = Get-Date -Format "dd/MM/yyyy HH:mm:ss" -Day 31 -Hour 07 -Minute 30 -Month 12 -Second 00 -Year 2021
PS> $Date1.GetType()

IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     True     String                                   System.Object

When you remove Format you receive DateTime and you can compare directly:

PS> $Date1 = Get-Date -Day 31 -Hour 07 -Minute 30 -Month 12 -Second 00 -Year 2021
PS> $Date1.GetType()

IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     True     DateTime                                 System.ValueType

PS> $Date2 = Get-Date -Day 31 -Hour 08 -Minute 30 -Month 12 -Second 00 -Year 2021
PS> $Date3 = Get-Date -Day 01 -Hour 08 -Minute 30 -Month 1 -Second 00 -Year 2022
PS> $date1 -lt $date2
True
PS> $date1 -lt $date3
True

when you use Get-Date with -Format powershell compares the first item in the format(in your case 'dd'). powershell comapres the date in the order in which you have mentioned the -format . for example if both the dates are equal then it will check months and so on. there is two solutions either you remove the format and use for example

$Date1=get-date 2021-31-02 
$date2=get-date 2022-01-01
if ($Date1 -lt $Date2){
    Write-Host "Date1 is before Date2" 
}
else{
    Write-Host "Date1 is after Date2"
}

or you can reverse the -format which is best suited for your use.

$Date1 = Get-Date -Format "yyyy/MM/dd HH:mm:ss" -Day 31 -Hour 07 -Minute 30 -Month 1 -Second 00 -Year 2021
$Date2 = Get-Date -Format "yyyy/MM/dd HH:mm:ss" -Day 01 -Hour 08 -Minute 30 -Month 1 -Second 00 -Year 2022 
 if ($Date1 -lt $Date2){
        Write-Host "Date1 is before Date2" 
    }
    else{
        Write-Host "Date1 is after Date2"
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM