How to parse (French) full month into datetime object in PowerShell?

后端 未结 1 410
Happy的楠姐
Happy的楠姐 2021-01-15 15:15

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         


        
相关标签:
1条回答
  • 2021-01-15 15:57

    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'))
    

    Note that I've had to change dd to d, since your input string's day index only has a single digit.


    By passing $null as the 3rd argument, you're implicitly using the current culture, as reflected in [cultureinfo]::CurrentCulture.[1]


    [1] The automatic $PSCulture variable works too, but only if the culture wasn't changed in-session.

    0 讨论(0)
提交回复
热议问题