1

I have a powershell script that prints a date, it does this via:

"$([DateTime]::Now)"

This produces a US format date of MM/DD/YYYY etc. I'm in the UK and would like this to come out as DD/MM/YYYY etc. I had thought that my server or shell was in the incorrect locale, so I added:

$culture = [system.globalization.cultureinfo]"en-GB"
[System.Threading.Thread]::CurrentThread.CurrentUICulture = $culture
[System.Threading.Thread]::CurrentThread.CurrentCulture = $culture

To the top of my script - no difference, indeed, checking the locale at startup or in the shell reports that my locale is "en-GB".

I have now tried the following:

"$([DateTime]::Now.ToString())"

Which gives me a UK format date. I have therefore acheived what I want to do, but this is now a mystery, why does Powershell's string interpolation of a DateTime return a different result than .ToString()? Is this a defined or controllable behaviour?

Cheers,

Mark

mklement0
  • 382,024
  • 64
  • 607
  • 775
Mark Rabjohn
  • 1,643
  • 14
  • 30
  • Because PowerShell's string interpolation routine does not default to `ToString()`, very simple :) – Mathias R. Jessen Oct 07 '21 at 14:09
  • Well, yes, I get that. What I can't find is an explanation of why Powershell ignores locale when it does string interpolation ona DateTime. What are the settings - is it hard-coded to en-US, can it be configured? – Mark Rabjohn Oct 07 '21 at 14:11
  • PowerShell by design uses the _invariant culture_ in expandable strings (string interpolation), casts, and implicit type conversions (but there are, unfortunately, exceptions). The behavior is _not_ configurable. [This answer](https://stackoverflow.com/a/37603732/45375) to the linked duplicate provides a hopefully comprehensive overview. – mklement0 Oct 07 '21 at 15:00
  • Applied to your example: `"$([DateTime]::Now)"` is the equivalent of `"$([DateTime]::Now.ToString([cultureinfo]::InvariantCulture))"` – mklement0 Oct 07 '21 at 15:05
  • @mklement0 - Thanks, Invariant Culture, that makes sense. I suppose that means that aside from dates, any floating point number also has to be specifically formatted in countries that do not use a comma as thousands separator and period as decimal separator. Seems tricky for some folk - I never noticed, because UK numbers are same as invariant. – Mark Rabjohn Oct 07 '21 at 15:06
  • Yes, but note that you only have to explicitly request culture-sensitive formatting when you convert from strings and use string interpolation. If you output a `[double]` value as-is, for instance, it _is_ formatted culture-sensitively. Ditto if you use of the [`-f` operator](https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_Operators#format-operator--f) instead of string interpolation. _Parsing_ strings with local-culture format requires explicit use of `::Parse($string)` calls (which default to culture-sensitivity, where supported, like `.ToString()`. – mklement0 Oct 07 '21 at 15:15
  • So basically $number or "$($number.ToString)" is thread culture, but "$number" isn't. I get it but it still seems odd. – Mark Rabjohn Oct 08 '21 at 08:34
  • If it helps... you could use `(Get-Date).GetDateTimeFormats()` to see all available options and then pick the one you want. Ex - `"The current date and time is $((Get-Date).GetDateTimeFormats()[12])"` – Dallas Oct 22 '21 at 22:41

0 Answers0