Compare dates

Hey

How do I: (compare)

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

(24H)

Mike

Powershell
LVL 1
mikeydkAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

NorieAnalyst Assistant Commented:
Mike

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

1
mikeydkAuthor Commented:
Hey

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

Open in new window

0
oBdACommented:
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

or
$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?
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.