Compare dates


How do I: (compare)

If ("01-01-2018 18:32:51" -gt "01-12-2017") { write-host "Hello World"}



Who is Participating?
oBdAConnect With a Mentor Commented:
In general, it's easiest to work with a DateTime provider here; saves writing down and relying on all those patterns. With the provider, the [DateTime]::Parse() method will try its very best to match the string to all kinds of different default formats matching the provider.
You can use [cultureinfo]::CurrentCulture.IetfLanguageTag if whatever generates these strings is localized based on the current Windows settings.
If the format is hard-coded, or differs from your default settings, you can use the respective IETF language identifier, which in your case would probably be "da-DK" or "de-DK".
The ParseExact() method is best suited when the string comes in some custom format that the provider can't identify out of the box.
$DTProvider = New-Object -TypeName System.Globalization.CultureInfo -ArgumentList ([cultureinfo]::CurrentCulture.IetfLanguageTag)

Open in new window

$DTProvider = New-Object -TypeName System.Globalization.CultureInfo -ArgumentList 'da-DK'

Open in new window

Admittedly, that's a lot to type, but should be used like that in scripts, where it's a one-time job; in the command line, you can abbreviate that to
$DTProvider = New-Object Globalization.CultureInfo 'da-DK'

Open in new window

Then just parse ahead:
$DTProvider = New-Object -TypeName System.Globalization.CultureInfo -ArgumentList 'da-DK'
$ReferenceDate = [datetime]::Parse("01-12-2017", $DTProvider)
If ([datetime]::Parse("01-01-2018 18:32:51", $DTProvider) -gt $ReferenceDate) {Write-Host "Hello World"}

Open in new window

The same can obviously be done if you know that some date string will be, for example, in American format (note how it parses all strings, without you having to specify the format explicitly):
$DTProvider = New-Object -TypeName System.Globalization.CultureInfo -ArgumentList 'en-US'
[datetime]::Parse("01/31/2018 11:22 pm", $DTProvider)
[datetime]::Parse("January 1, 2018 11:22pm", $DTProvider)
[datetime]::Parse("Jan 2 18 23:45", $DTProvider)

Open in new window

But then again, PowerShell should use your default DateTime provider anyway when using the cast that Norie suggested. Or are you working on an English system, parsing output in Danish format?
NorieVBA ExpertCommented:

Not 100% sure what you are after but try this, it will convert the 'text' date/time values you have into real date/time values and compare them.
If ([datetime]"01-01-2018 18:32:51" -gt [datetime]"01-12-2017") { write-host "Hello World"} 

Open in new window

mikeydkAuthor Commented:

Great - but how do I type cast the string - when using european format? (dd-MM-yyyy)
David Johnson, CD, MVPConnect With a Mentor OwnerCommented:
please note the backticks inserted for readability
If ( `
([datetime]::ParseExact("01-01-2018 18:32:51",'dd-MM-yyyy HH:mm:ss',$null)) -gt `
  write-host "Hello World"

Open in new window

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.