ome*_*ega 2 powershell datetime
我有一个日期字符串,我想解析为datetime对象.我有这个:
$invoice = '9 février 2017'
[datetime]::parseexact($invoice, 'dd MMMM yyyy', $null)
Run Code Online (Sandbox Code Playgroud)
但它不起作用.如何将月份(总是法国BTW)转换为日期时间对象?
第三个参数DateTime.ParseExact是IFormatProvider,你可以CultureInfo在其上下文中传递一个实例来解释字符串,所以你必须传递代表法国文化的对象:
[datetime]::ParseExact($invoice, 'd MMMM yyyy', [cultureinfo]::GetCultureInfo('fr-FR'))
Run Code Online (Sandbox Code Playgroud)
请注意,我必须更改dd为d,因为您的输入字符串的日期索引只有一个数字.
通过传递$null作为第三个参数,你隐含地使用当前的文化,如反映[cultureinfo]::CurrentCulture.[1]
[1]自动$PSCulture变量也有效,但前提是文化没有在会话中更改.