I have the following in an array variable
Mon May 1 08:13:18 2017 Sat Apr 29 19:07:14 2017
In order to update the format and timezone, I have to run these two ParseExact commands first.
$time | ForEach-Object {$_ = [datetime]::ParseExact($_,'ddd MMM d HH:mm:ss yyyy',[System.Globalization.CultureInfo]::InvariantCulture)}
$time | ForEach-Object {$_ = [datetime]::ParseExact($_,'ddd MMM dd HH:mm:ss yyyy',[System.Globalization.CultureInfo]::InvariantCulture)}
I get an error on the one that fails (which is expected)
Exception calling "ParseExact" with "3" argument(s): "String was not recognized as a valid DateTime."
At D:\UPDATING.ps1:
+ ... ach-Object {$_ = [datetime]::ParseExact($_ ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : FormatException
Everything works in the long run and I can do my formatting, but I would like to know if there is a better of doing this rather than using 2 parseExacts
$time | % { [DateTime]::ParseExact($_, [string[]]('ddd MMM d HH:mm:ss yyyy', 'ddd MMM dd HH:mm:ss yyyy'), [CultureInfo]::InvariantCulture, 'None') }or$time | % { [DateTime]::ParseExact($_, 'ddd MMM d HH:mm:ss yyyy', [CultureInfo]::InvariantCulture, 'AllowInnerWhite') }$time | % { [DateTime]::ParseExact($_, 'ddd MMM d HH:mm:ss yyyy', [CultureInfo]::InvariantCulture, 'AllowInnerWhite') }@PetSerAl If you put it in to an answer I'll mark it.