如何在PowerShell中将整个月解析(法语)为datetime对象? [英] How to parse (French) full month into datetime object in PowerShell?
问题描述
我有一个日期字符串,我想将其解析为datetime对象。我有这个:
I have a string of a date which I want to parse into a datetime object. I have this:
$invoice = '9 février 2017'
[datetime]::parseexact($invoice, 'dd MMMM yyyy', $null)
但这不起作用。如何将月份(始终是法国BTW)转换为日期时间对象?
But it doesn't work. How can I convert the month (which is always French BTW) into the date time object?
推荐答案
DateTime.ParseExact
是 IFormatProvider
,您可以将 CultureInfo
实例,在该实例中,字符串应为解释,因此您必须将代表法国文化的对象传递给它:
The 3rd argument to DateTime.ParseExact
is the IFormatProvider
, to which you can pass a CultureInfo
instance in whose context the string should be interpreted, so you must pass it the object representing French culture:
[datetime]::ParseExact($invoice, 'd MMMM yyyy', [cultureinfo]::GetCultureInfo('fr-FR'))
请注意,我ve必须将 dd
更改为 d
,因为您输入的字符串的日索引只有一位数字。
Note that I've had to change dd
to d
, since your input string's day index only has a single digit.
通过传递 $ null
作为第三个参数,您就隐含了y使用 current 文化,如 [cultureinfo] :: CurrentCulture
所示。 [1]
By passing $null
as the 3rd argument, you're implicitly using the current culture, as reflected in [cultureinfo]::CurrentCulture
.[1]
[1]自动 $ PSCulture
变量也可以,但是只有在会议期间文化没有改变的情况下。
[1] The automatic $PSCulture
variable works too, but only if the culture wasn't changed in-session.
这篇关于如何在PowerShell中将整个月解析(法语)为datetime对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!