How do I convert a string date held in a variable to a datetime format.
The variable $stringdate contains the string '2020-04-10 09:15:30' when I run the commands (1) below I get the following error:
InvalidArgument: Cannot convert value "2020-04-10 09:15:30" to type "System.DateTime"`.
Error: "String '2020-04-10 09:15:30' was not recognized as a valid DateTime."
If I run commands (2) with the string "2020-04-10 09:15:30" instead of the variable $stringdate it doesn't error and returns 10 April 2020 09:15:30??
#(1)
$date = $stringdate
[datetime]$date
# (2)
$date ="2020-04-10 09:15:30"
[datetime]$date
$stringdatein the first place?$stringdate = '2020-04-10 09:15:30'; [datetime] $stringdateworks fine, the implication is that the$stringdateread from a file contains something else - perhaps invisible control characters.