Get-Date Multi Language PowerShell

Get-Date Multi Language PowerShell

Hello,

Today I wanted to share a nice trick that will allow you to write the full date in another language than the one from your current Windows configuration. This method will work with any DateTime object and all of the cultures supported by your .Net version.

Get-Date in a different language with PowerShell

By default, Get-Date will return the dates based on your system language:

Get-Date Multi Language PowerShell - Get-Date
Localized Get-Date

If you want to write this date in Spanish, it’s a bit more complicated, you need to use the .Net class named CultureInfo. This will contains other languages months and days names:

 Get-Date Multi Language PowerShell - Spanish day & month names
Spanish day & month names
$ES = New-Object System.Globalization.CultureInfo("es-ES")
$ES.DateTimeFormat.DayNames
$ES.DateTimeFormat.MonthNames

Then, with this in mind, we can build something a bit more complex like this:

Get-Date Multi Language PowerShell - Spanish date built from a French system
Get-Date Multi Language PowerShell – Spanish date built from a French system
"{0} {1} {2}" -f $es.DateTimeFormat.DayNames[$Date.DayOfWeek.value__],$es.DateTimeFormat.MonthNames[$Date.Month-1],$Date.Year

This line uses the “-f” format operator to allow us to search through the $ES variable for the localized names of month & day based on the current date.

You do this with other languages such as:

  • EN-US
  • RU-RU
  • DE-DE
  • Etc…

The full list of possible languages is available with:

[System.Globalization.CultureInfo]::GetCultures("AllCultures")

Conclusion

You can now build some date in every format supported by .Net, very convenient if you need to send customized emails with long date format in different languages.

Edit: A reader pointed to me that there is kind of an easier way to do it:

([DateTime]"01/01/2020").ToString('D',[CultureInfo]::CreateSpecificCulture("es-es"))

This is achieved thanks to an overload of the ToString method:

Get-Date Multi Language PowerShell - ToString Overloads
Get-Date Multi Language PowerShell – ToString Overloads

Thank you JB for your feedback 🙂

One thought on “Get-Date Multi Language PowerShell

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.