Check if the date is past

$olddate = Get-Date -Date "31-dec-2013 00:00:00" -Format dMyyyy $Now = Get-date -Format dMyyyy 

How to check if $ olddate exists before $ Now?

+5
source share
2 answers

If you save them as DateTime instead of formatted strings, you can use the smaller operator ( -lt ) just like with regular numbers, and then use the format operator ( -f ) when you need to actually display the date:

 $olddate = Get-Date -Date "31-dec-2013 00:00:00" $Now = Get-Date if($olddate -lt $Now){ "`$olddate is in the past!" "{0:dMyyyy}" -f $olddate "{0:dMyyyy}" -f $Now } 
+5
source

You can also do a comparison on Ticks. Ie: $olddate.Ticks will be -lt , then $Now.Ticks .

+1
source

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


All Articles