3

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

3
  • 3
    $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') } Commented May 1, 2017 at 12:36
  • @PetSerAl Oooooo. That is so much better I forgot it could take multiple arguments. Commented May 1, 2017 at 14:45
  • all the answers provided worked. I am going to using this $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. Commented May 2, 2017 at 3:56

2 Answers 2

3

There are multiple overloads of ParseExact method. One of them allows you to specify multiple patterns of date to parse:

$time = 'Sat Apr 29 19:07:14 2017', 'Mon May  1 08:13:18 2017'
$time | % { [DateTime]::ParseExact($_, [String[]]('ddd MMM  d HH:mm:ss yyyy',
                                                  'ddd MMM dd HH:mm:ss yyyy'), [CultureInfo]::InvariantCulture, 'None') }

Also there are overloads, which allow you to specify additional parse options, like [Globalization.DateTimeStyles]::AllowInnerWhite, to allow additional whitespace characters inside parsed string:

$time | % { [DateTime]::ParseExact($_, 'ddd MMM d HH:mm:ss yyyy', [CultureInfo]::InvariantCulture, 'AllowInnerWhite') }
Sign up to request clarification or add additional context in comments.

Comments

2

There is an extra space in your first date and a single digit day, so the pattern would actually need to be ddd MMM d HH:mm:ss yyyy.

Full command:

[datetime]::ParseExact($_,'ddd MMM  d HH:mm:ss yyyy',[System.Globalization.CultureInfo]::InvariantCulture)

However, this isn't the case for your second date. A single d will still work for this but the changing in space is an issue you might need to program around. One solution would be to replace all double spaces with singles first.

This seems to work for both dates, here's a proof of concept:

$dates = "Sat Apr 29 19:07:14 2017","Mon May  1 08:13:18 2017"
$dates = $dates -replace '  ',' '

$dates | ForEach-Object {
    [datetime]::ParseExact($_,'ddd MMM d HH:mm:ss yyyy',[System.Globalization.CultureInfo]::InvariantCulture)
}

6 Comments

I don't think the extra space is the root of the problem. You can see it is accounted for in the question with the first of the two parse statements. I think the actual issue is with the variable integer days which can be one digit or two depending. Looks like the OP runs both commands against the strings knowing one will succeed and expects the other to fail.
@Matt yes, that is correct. Mark's response is a way around the issue by deleting the "extra" space in the day
@mattnicola What is it suppose to be? It says Mon in the question?
It's now a date time object so you need to use -format to reformat the output if you want to.
Maybe a different Q, (and its not really an issue for me), but why is the output now in this format Saturday, April 29, 2017 7:07:14 PM
|

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.