Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to parse datetime by ::parseexact in PowerShell?

Tags:

powershell

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

like image 867
shagrath9212 Avatar asked May 07 '18 13:05

shagrath9212


People also ask

What does DateTime ParseExact do?

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.

How do I convert DateTime in PowerShell?

Use DateTime ParseExact() method or cast to Datetime format to convert string to DateTime.

How do I get the current time in PowerShell?

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.

What formats does DateTime parse?

Only the ISO 8601 format ( YYYY-MM-DDTHH:mm:ss.sssZ ) is explicitly specified to be supported.


1 Answers

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.

like image 179
Vivek Kumar Singh Avatar answered Oct 05 '22 19:10

Vivek Kumar Singh