7
votes

I'v got a problem when trying to parse, and convert string to [DateTime] format in PowerShell using ::parseexact. Can someone tell me where's my bad? Here's my code.

[datetime]::parseexact('2018-05-07T15:19:17.839+03:00','o', 'yyyy-MM-ddTHH:mm:ss.fffzzz')

And here's an error

Cannot find an overload for "parseexact" and the argument count: "3". At line:1 char:1 + [datetime]::parseexact('2018-05-07T15:19:17.839+03:00','o', 'yyyy-MM- ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (:) [], MethodException + FullyQualifiedErrorId : MethodCountCouldNotFindBest

1
[datetime]::parseexact('2018-05-07T15:19:17.839+03:00', 'yyyy-MM-ddTHH:mm:ss.fffzzz',$null)user6811411
Thanks! It works!shagrath9212

1 Answers

6
votes

As mentioned in the comment, you can change your code to [datetime]::parseexact('2018-05-07T15:19:17.839+03:00', 'yyyy-MM-ddTHH:mm:ss.fffzzz',$null) and that will work for you if you are not concerned with the CultureInfo.

But as I can see in your code, that you are providing 3 arguments to your ParseExact function, which makes me wonder if you are really trying to change the CultureInfo. If you want to change your that, you can do something like this -

([datetime]::ParseExact($date,"dd/MM/yyyy",[Globalization.CultureInfo]::CreateSpecificCulture('en-GB'))

OR

([datetime]::ParseExact($date,"dd/MM/yyyy",[Globalization.CultureInfo]::CreateSpecificCulture('de-DE'))

and so on depending upon your requirement.

Extra Info -

As per the msdn article,

The DateTime.ParseExact(String, String[], IFormatProvider, DateTimeStyles) method parses the string representation of a date that matches any one of the patterns assigned to the formats parameter. If the string s does not match any one of these patterns with any of the variations defined by the styles parameter, the method throws a FormatException. Aside from comparing s to multiple formatting patterns, rather than to a single formatting pattern, this overload behaves identically to the DateTime.ParseExact(String, String, IFormatProvider, DateTimeStyles) method.

The style parameter, is a bitwise combination of enumeration values that indicates the permitted format of s(input date). A typical value to specify is None.

See the Get-Date Formatting Culture for more info.