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
ParseExact(String, String, IFormatProvider) Converts the specified string representation of a date and time to its DateTime equivalent using the specified format and culture-specific format information. The format of the string representation must match the specified format exactly.
Use DateTime ParseExact() method or cast to Datetime format to convert string to DateTime.
Get-Date uses the UFormat parameter with format specifiers to display the current system date and time. The format specifier %Z represents the UTC offset of -07. The $Time variable stores the current system date and time. $Time uses the ToUniversalTime() method to convert the time based on the computer's UTC offset.
Only the ISO 8601 format ( YYYY-MM-DDTHH:mm:ss.sssZ ) is explicitly specified to be supported.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With