为什么 PowerShell 将 DateTime 格式字符串中的斜杠视为点? [英] Why PowerShell treats slash in DateTime format string as dot?
问题描述
PS C:\> (Get-Date).ToString("MM/dd/yyyy")
11.12.2016
PS C:\> [DateTime]::ParseExact('10/14/2016', 'MM/dd/yyyy', $null)
Exception calling "ParseExact" with "3" argument(s): "String was not recognized as a valid DateTime."At line:1 char:1
+ [DateTime]::ParseExact('10/14/2016', 'MM/dd/yyyy', $null)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : FormatException
PS C:\> [DateTime]::ParseExact('10.14.2016', 'MM/dd/yyyy', $null)
14 Oct 2016 0:00:00
PS C:\> $PSVersionTable.PSVersion
Major Minor Build Revision
----- ----- ----- --------
5 0 10586 117
它在 Windows Server 2008 R2 上.在 Windows 10 上的版本是 5.1.14393.206,它可以正常工作.
It is on Windows Server 2008 R2. On Windows 10 version is 5.1.14393.206 and it works correctly.
这是怎么回事?
推荐答案
As Jeroen Mostert 在对您的问题的评论中指出,正斜杠在日期/时间格式字符串中具有特殊含义.
As Jeroen Mostert pointed out in the comments to your question, the forward slash has a special meaning in date/time format strings.
来自文档:
/"自定义格式说明符代表日期分隔符,用于区分年、月和日.从当前或指定区域性的 DateTimeFormatInfo.DateSeparator
属性中检索适当的本地化日期分隔符.
The "/" custom format specifier
The "/" custom format specifier represents the date separator, which is used to differentiate years, months, and days. The appropriate localized date separator is retrieved from the
DateTimeFormatInfo.DateSeparator
property of the current or specified culture.
注意
要更改特定日期和时间字符串的日期分隔符,请在文字字符串分隔符中指定分隔符.例如,自定义格式字符串 mm'/'dd'/'yyyy
生成的结果字符串中始终使用/"作为日期分隔符.要更改区域性所有日期的日期分隔符,请更改当前区域性的 DateTimeFormatInfo.DateSeparator
属性的值,或实例化一个 DateTimeFormatInfo
对象,分配字符到其 DateSeparator
属性,并调用包含 IFormatProvider
参数的格式化方法的重载.
Note
To change the date separator for a particular date and time string, specify the separator character within a literal string delimiter. For example, the custom format stringmm'/'dd'/'yyyy
produces a result string in which "/" is always used as the date separator. To change the date separator for all dates for a culture, either change the value of theDateTimeFormatInfo.DateSeparator
property of the current culture, or instantiate aDateTimeFormatInfo
object, assign the character to itsDateSeparator
property, and call an overload of the formatting method that includes anIFormatProvider
parameter.
要匹配文字正斜杠而不是系统区域设置中定义的日期分隔符,请使用反斜杠对其进行转义 ('MM\/dd\/yyyy'
).
For matching a literal forward slash instead of the date separator defined in the system's regional settings escape it with a backslash ('MM\/dd\/yyyy'
).
此外,不建议为格式提供程序传递 $null
.为日期字符串指定正确的culture,或使用 InvariantCulture
如果您希望独立于特定文化来解析字符串.
Also, passing $null
for the format provider is not recommended. Either specify the correct culture for the date string, or use InvariantCulture
if you want the string parsed independent of a particular culture.
[DateTime]::ParseExact('10/14/2016', 'MM\/dd\/yyyy', [Globalization.CultureInfo]::InvariantCulture)
这篇关于为什么 PowerShell 将 DateTime 格式字符串中的斜杠视为点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!