1

In powershell I have a date that I expect is a string and another that I created for debugging purposes which is a string.

function ParseDate {
param
(
    $inputDate #when debugging this outputs - 04/11/2021 23:00
)
    $date = "04/11/2021 23:00" #hard coded for debugging

    $parsedDate = [DateTime]::MinValue;
    # $date here, which I had for debugging, $inputDate will fail for parse 
    $parsedSuccessfully = [DateTime]::TryParseExact($date, "dd/MM/yyyy HH:mm", $null, [System.Globalization.DateTimeStyles]::None, [ref] $parsedDate);  

    return $parsedDate 
}

If I parse the hardcoded date ($date) then it works fine, but if I parse the $inputDate it fails and $parsedSuccessfully will be false.

If I output the GetType() on both objects then it returns the same type -

IsPublic IsSerial Name                                     BaseType                                                                                                                  
-------- -------- ----                                     --------                                                                                                                  
True     True     String                                   System.Object  

Is there any way to tell what the difference is between the inputDate and hard coded date as something must be different as one works and the other does not.

11
  • How you are calling this function? Commented Nov 5, 2021 at 18:05
  • I'm pulling out the date from the metadata of a file (date taken of a photo) $fileDate= $objFolder.getDetailsOf($f, 12) $date = ParseDate $fileDate . I did try it with this in the method header, same thing param ( [string] $inputDate ) Commented Nov 5, 2021 at 18:07
  • Unfortunately I am not able to reproduce this issue Commented Nov 5, 2021 at 18:11
  • Verify the contents with something like .Length. Index the individual characters if necessary. Pay special attention to the last one. Commented Nov 5, 2021 at 18:19
  • 2
    Try [BitConverter]::ToString([Text.Encoding]::UTF8.GetBytes($date)) to see the UTF8-encoded code points of the characters involved. Commented Nov 5, 2021 at 18:51

1 Answer 1

0

See this answer here - https://stackoverflow.com/a/67228497/440760 Yes this was also my question!

Essentially use write-host( $tmp.ToCharArray() | % { [int] $_ }) to show the text in hex. This clearly shows that there are differences.

In my case - 8206 48 52 47 8206 49 49 47 8206 50 48 50 49 32 8207 8206 49 51 58 49 57 (didn't work) AND 48 52 47 49 49 47 50 48 50 49 32 49 51 58 49 57 (worked).

The extra chars are BSTR which can be removed using

$formattedDateString = $value -replace '[^\p{L}\p{Nd}\:\/\ ]', ''
Sign up to request clarification or add additional context in comments.

1 Comment

moving my comment here so I can delete my answer: It's very strange that in this, and your older question, you have these errant characters ending up in your strings. Rather than relying on string manipulation to fix it, I would recommend investigating why you have this strange situation in the first place.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.