1

I'm stumped. :)

My computer has PowerShell 5.1 installed. On another computer (same language) 5.0 it works as expected. (Check using Get-Culture; my locale is nb-NO (Norwegian) )

Consider this:

Get-Date

returns

tirsdag 23. mai 2017 13.13.18

So I do this

Get-Date -Format "H-m-s"

as expected it returns

13-13-18

But then I do this

Get-Date -Format "H:m:s"

You think it returns

13:13:18

right? (it does on PS5.0!) No! I get this:

13.13.18

Only if I do this, is the output what I want:

Get-Date -Format "H\:m\:s"
13:13:18

Can someone please explain why this is? I discovered it "by accident" when I wanted to format a datetime-compatible string for use in SQL Server.

3
  • 1
    I'd simply use Get-Date -Format "T" Commented May 23, 2017 at 11:30
  • Sorry, that doesn't solve anything, it still returns as 13.13.18 for me. Commented May 23, 2017 at 11:42
  • Well not everyone knows the time separator from nb-NO out of the head, so precede my above command with @Mathias tip [CultureInfo]::CurrentCulture.DateTimeFormat.TimeSeparator = ":" Commented May 23, 2017 at 11:48

1 Answer 1

1

That's because the underlying DateTime formatting function see's : and treats it as a culture-dependent "time separator".

In Norwegian (no-NO), the default time separator is .. You can inspect this with (assuming that no-NO is the current culture):

PS C:\> [CultureInfo]::CurrentCulture.DateTimeFormat.TimeSeparator
.

You can also override this, either by inserting a literal :, with the escape sequence \: as you've already found, or you can override it globally (for the lifetime of the current process/appdomain):

PS C:\> [CultureInfo]::CurrentCulture.DateTimeFormat.TimeSeparator = ":"
PS C:\> Get-Date -Format "H:m:s"
13:13:18
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks! The literal ":" doesn't do it for me, but using the escape sequence does. Also, why has this changed since PS 5.0?
Sorry, was being unclear - if you want a literal : in the resulting string, you need to use the escape sequence. AFAIK, it's not because of any changes in PowerShell, but because of the underlying DateTime formatting implementation differing between .NET 3.5 (CLR 2.0) and .NET 4.5 (CLR 4.0), no idea why

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.