It’s back, your favorite Friday PowerShell diversion!
The get-date command has a useful method called GetDateTimeFormats() that lists all of the available date-time formats available on your system, suitable for you to find just the right date format to fit your needs. However, actually using the output can be difficult, as you must use the following syntax, (Get-Date).GetDateTimeFormats()[<index>] and picking the right one from a huge non-numbered list can be challenging.
Your Mission
Display all of the dateTime formats available, along with the index position of each in a graphical GUI user interface for the user to pick the format they desire, then use that selection to display a tip to the user on how to get that particular format.
Rules
- Try to avoid using Semicolons. If you have to use one, explain it.
- You are encourage to use backticks and PowerShell next-line detection (brackets, pipes) to wrap your code in to a readable one-liner
- Interpret GUI and ‘display a tip’ however you like
- No prizes, no whining!
How about this? (This makes an assumption about the indexes matching, so I would recommend using the Get-Date -Format ‘xxxx’ version over the GetDateTimeFormats()[x] version):
[Globalization.DateTimeFormatInfo]::CurrentInfo.GetAllDateTimePatterns() | ForEach-Object {
$i = 0
$Now = Get-Date
} {
[PSCustomObject] @{
Index = $i++
FormattedString = $Now.ToString($_)
Pattern = $_
}
} | Out-GridView -Title “Choose the desired format(s)” -PassThru |
ForEach-Object { @”
{0} = {{
Get-Date -Format ‘{1}’
-or-
(Get-Date).GetDateTimeFormats()[{2}]
}}
“@ -f $_.FormattedString, $_.Pattern, $_.Index
}
LikeLike
SLICK! I never thought about incrementing the index from within an object declaration! I actually had to use a semicolon in my approach!
LikeLike
I like your use of [Globalization.DateTimeFormatInfo]. I don’t think I’ve used that before. It let me alter my take on the challenge to provide a more meaningful example format:
[Globalization.DateTimeFormatInfo]::CurrentInfo.GetAllDateTimePatterns() | Sort-Object -Unique | foreach {$index = 0} { [PSCustomObject]@{
Index = “$index.”
Format = $_
Example = $Now.ToString($_)
}
$index++
} | Out-GridView -Title “Please select your format” -OutputMode Single | Select Index,Format,Example
LikeLike
Now I can’t stop myself. I like this one better because it’s almost impossible to read (as are all appropriate one-liners):
$(for($i=0;$i -lt [Globalization.DateTimeFormatInfo]::CurrentInfo.GetAllDateTimePatterns().count;$i++){ [PSCustomObject]@{
Index=$i
Format=[Globalization.DateTimeFormatInfo]::CurrentInfo.GetAllDateTimePatterns()[$i]
Example = $Now.ToString([Globalization.DateTimeFormatInfo]::CurrentInfo.GetAllDateTimePatterns()[$i])}
}
) | Out-GridView -OutputMode single -Title ‘Pick your format’
LikeLike
Cool idea!
This is the best I can come up with:
0..(Get-Date).GetDateTimeFormats().Count |
ForEach-Object { @{“$_” = “$((Get-Date).GetDateTimeFormats()[$_])”} } |
Out-GridView -PassThru |
ForEach-Object {‘Get the date format “{0}” with the following command: “(Get-Date).GetDateTimeFormats()[{1}]”‘ -f $_.Value,$_.Name }
LikeLike
Very clever! I tried doing this angle with a for($i =0 ;$i -gt (Get-Date).GetDateTimeFormats().Count; $i++), but got burned because the For Doesn’t emit pipeline output. Very Clever.
LikeLike
Challenge accepted!
$(for($i=0;$i -lt (get-date).GetDateTimeFormats().count;$i++){ [PSCustomObject]@{
Index=$i
Format=(get-date).GetDateTimeFormats()[$i]} } ) | Out-GridView -OutputMode single -Title ‘Pick your format’
LikeLike
Very weird… I came to almost the exact same solution:
0..(((get-date).GetDateTimeFormats()).Count-1) |
%{@{$_ = ((Get-Date).GetDateTimeFormats()[$_]).tostring()}} |
Out-GridView -PassThru -Title “Which format(s) would you like?” |
%{Write-Output (“`”{0}`” can be made by running: (Get-Date).GetDateTimeFormats()[{1}]” -f $_.Value,$_.Name)}
LikeLike
Loved the challenge!
0..((Get-Date).GetDateTimeFormats().Count – 1) |
ForEach-Object {
New-Object -TypeName PSObject -Property @{
Index = $_
‘Time Format’ = (Get-Date).GetDateTimeFormats()[$_]
}
} |
Out-GridView -PassThru |
ForEach-Object {
Write-Output “You’ve selected the date format: $($_.’time format’)!”
}
I have a tendency to make objects rather than do string formatting which looks like the others’ used! I need to re-evaluate EVERYTHING lool
LikeLike
I make objects too, but I use the casting constructor of [pscustomobject]@{ColumnHeading=} instead. Your method works fine still!
LikeLike
I had a semicolon in the hashtable because I was defining two properties. But then I saw Sean Quinlan’s post and it made me realize that I could do it with one property.
(Get-Date).GetDateTimeFormats() | ForEach-Object -Begin{$index = 0} {@{“$_”= $index++}} | Out-GridView -OutputMode Single | %{“(Get-Date).GetDateTimeFormats()[{0}]” -f $_.value}
LikeLiked by 1 person
OK, you guys are all pretty much better than I am…I was previously proud of my approach to this, but after seeing what you guys can do, I’m not sure I wanna post it anymore 🙂
LikeLike
This is what I came up with if I understood the rules right :). I uniqued the time formats because there looked like there were duplicates in there.
(get-date).GetDateTimeFormats() | Get-Unique | foreach {$index=0} {
[pscustomobject]@{
Index=”$index.”
DateFormat=$_}
$index++} | Out-GridView -Title ‘Please pick a date format’ -OutputMode Single | Select DateFormat
LikeLike
Wow, I had no idea that you could do that in a foreach!
LikeLike
That’s the great thing about stuff like this. We all get to share 🙂 (and then come back to it a year later when we forget how to do it..I know I refer back to my scripting games submissions every now and then)
LikeLike
Is this cheating? 😛
{ Write-Output “Enter number of a format you like: ” },`
{ param($formats) for ($i = 0; $i -lt $formats.Count; $i++){ “$($i): ” + $formats[$i] } },`
{ param($formats) Write-Output “You selected $($formats[(Read-Host)])!” }`
| %{ Invoke-Command $_ -Args (,(Get-Date).GetDateTimeFormats()) }
LikeLike
I’ll have to run this in the console…
LikeLike