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.

delp
  • 771
  • 11
  • 20
  • How you are calling this function? – Abdul Niyas P M Nov 05 '21 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 ) – delp Nov 05 '21 at 18:07
  • Unfortunately I am not able to reproduce this issue – Abdul Niyas P M Nov 05 '21 at 18:11
  • Verify the contents with something like `.Length`. Index the individual characters if necessary. Pay special attention to the last one. – Jeroen Mostert Nov 05 '21 at 18:19
  • This means the strings clearly are **not** identical, so you should try to find out where the difference is. Compare individual characters, if necessary, pay attention to stray whitespace. – marsze Nov 05 '21 at 18:26
  • @JeroenMostert Thanks, yes I can see one is 21 chars and the other is 16. So clearly something extra is in there. Do you know how to show whitespace? I tried Trim(), but after that the 21 char string, still had a length of 21. – delp Nov 05 '21 at 18:45
  • what is `$objFolder` ? – antonyoni Nov 05 '21 at 18:48
  • 2
    Try `[BitConverter]::ToString([Text.Encoding]::UTF8.GetBytes($date))` to see the UTF8-encoded code points of the characters involved. – Jeroen Mostert Nov 05 '21 at 18:51
  • is this your real function? is `$inputDate` the real name of the parameter? Are you sure you haven't named it `$input` for example? – briantist Nov 05 '21 at 18:51
  • I found a way to show all chars - write-host( $date.ToCharArray() | % { [int] $_ }) Also found that if I do this - $formattedDateString = $dateX -replace '[^\p{L}\p{Nd}\:\/\ ]', '' then I can strip out the extra chars. then it works as expected. – delp Nov 05 '21 at 18:55
  • Also a bit of a doh! moment when looking at my question/problem again as it was looking very familiar (this is 1st time I've looked at powershell since April, and I was a noob then...) https://stackoverflow.com/a/67228497/440760 I'll stick this into the answers, can't believe I've ask essentially the same Q from different bits of code! (sorry...) – delp Nov 05 '21 at 18:58

1 Answers1

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}\:\/\ ]', ''
delp
  • 771
  • 11
  • 20
  • 5
    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. – briantist Nov 05 '21 at 19:05