Why does powershell insist that 18 is less than 9?

Screenshot

Why is this so? (The Get date is set to 18 right now ...) Maybe something with the way Get-Date formats the number? I'm at a dead end ....

+4
source share
2 answers

As already mentioned, you are comparing strings, not comparing numbers

$Time = Get-Date -Format %H
$Time.GetType()

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

To do what you want, you can make your $Timenumber

[int]$Time -lt 9 
#or with a little trick 
+$Time -lt 9
+7
source

Get-Date -Format returns a string.

This means that you are comparing the 2-letter string "18" with 9.

The operator -lt(like any other comparison operator in PowerShell), having seen the string as an argument of the left hand, will also try to convert the right argument to a string, so the comparison is effective:

"18" -lt "9"

"1" "9" , $true

+5

Source: https://habr.com/ru/post/1675659/


All Articles