Вопрос

As far as I know the forward slash isn't a special character in PowerShell, but in this particular situation the forward slash causes a problem.

Write-Host $([DateTime]::Now.ToString("MM/dd yyyy"))

In this case ToString ignores the custom date format (probably because it can't parse it) and outputs date in the default format.

I know how to solve the problem - I can escape the forward slash with a backward slash "MM\/dd yyyy", but I'd like to know why it is necessary.

Это было полезно?

Решение

This is not PowerShell doing it, but the .Net framework. / in a date format is seen as the date separator and will be replaced with the date separator set in the Windows settings. The escape for that is a backslash as you noticed.

Write-Host $([DateTime]::Now.ToString("MM\/dd yyyy"))
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top