Can yyyy-MM-dd give different dates in PowerShell?

November 15, 2026 3 answers
Asked by Nivrick.8j

I use yyyy-MM-dd when formatting dates in PowerShell. Can the result change depending on the system's culture, even though the format string is explicit?

Answers

~Tavlo+8r

Yes. The format specifies the order and width of the fields, but the culture can also determine which calendar supplies the year, month, and day. A Thai culture, for example, can format the year using the Buddhist calendar rather than the Gregorian year you expected.

For a stable, culture-independent result, pass the provider explicitly: (Get-Date).ToString('yyyy-MM-dd', [cultureinfo]::InvariantCulture). That uses the invariant culture's Gregorian calendar.

w+Nolven.39:

Passing $null as the provider is not the same thing. DateTime.ToString falls back to the current culture when the provider is null.

Reply to this answer
Mivrel!6q

This is worth testing if the string goes into a filename, comparison, or data exchange. Keep one DateTime value and format it with several CultureInfo objects, then compare each result with the invariant version. Get-Culture -List can provide the cultures available on your system.

That checks the actual formatting behavior without changing the date between tests. The available cultures and their results can vary by environment.

Reply to this answer
Tavrel-2x

Culture and time zone are separate issues. InvariantCulture makes the formatting predictable, but it doesn't convert a local time to UTC or add an offset. Decide which time zone the value should represent before formatting it.

Reply to this answer

Add your answer

Answers are reviewed before publication.

Related Discussions