mzv
mzv

Reputation: 307

Check if date in past

$olddate = Get-Date -Date "31-dec-2013 00:00:00" -Format d.M.yyyy
$Now = Get-date -Format d.M.yyyy

How can I check if $olddate is earlier than $Now?

Upvotes: 6

Views: 8304

Answers (2)

David Brabant
David Brabant

Reputation: 43499

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

Upvotes: 1

Mathias R. Jessen
Mathias R. Jessen

Reputation: 174505

If your store them as DateTime instead of as formatted strings, you can use the less-than 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:d.M.yyyy}" -f $olddate
    "{0:d.M.yyyy}" -f $Now
}

Upvotes: 6

Related Questions