Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Force Get-Date to use '/' as date separator

Tags:

powershell

How can I get Get-Date to produce '/' as the date separator? There is a requirement to produce a en-US centric date format. It appears that PowerShell will always use the regional settings regardless of the format specified. It is set to '-' on my system, but I need to produce a date using '/'.

PS C:\src> Get-Date -Format MM/dd/yyyy
06-26-2017
PS C:\src> Get-Date -Format "MM/dd/yyyy"
06-26-2017
PS C:\src> Get-Date -Format 'MM/dd/yyyy'
06-26-2017
PS C:\src> Get-Date -Format "MM`/dd`/yyyy"
06-26-2017

The date separator is set to '-' on my machine. I do not find a Set-UICulture command.

PS C:\src> (Get-UICulture).DateTimeFormat.DateSeparator
-
like image 643
lit Avatar asked Sep 05 '25 05:09

lit


1 Answers

In the format string, / represents the current UICulture date separator. To force it to be explicitly the / character, you must escape it using the \ character:

Get-Date -Format "MM\/dd\/yyyy"
like image 78
Jeff Zeitlin Avatar answered Sep 07 '25 20:09

Jeff Zeitlin