3

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)

But it doesn't work. How can I convert the month (which is always French BTW) into the date time object?

1
  • not sure if its related but $null in ps != null in c# Commented Feb 27, 2017 at 22:28

2 Answers 2

11

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.

Sign up to request clarification or add additional context in comments.

Comments

0
$invoice = "Dimanche 21 Janvier 2024"
$date = [datetime]::ParseExact($invoice, 'dddd dd MMMM yyyy', [cultureinfo]::GetCultureInfo('fr-FR'))

Using the format 'dddd dd MMMM yyyy', the date will be parsed correctly

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.