Powershell ParseExact "String was not recognized as a valid DateTime"

Viewed 279

I am using Powershell to pull data from a web application through a REST API and I need to convert the date/time to a different format from the result. The time is in an ISO combined date-time format (i.e 2019-06-24T03:08:21) and i would like it to be "yyyyMMdd". The data is received in JSON format

I have tried using an exact date instead of the $WebResponse.occurred variable and it still errors out.

Code:

$BeginDate = (Get-Date).AddDays(-7).ToString("yyyyMMdd")
$EndDate = Get-Date -Format yyyyMMdd
$WebResponse = Invoke-RestMethod -H @{'Authorization' = 'Token **'} https://webhost.com/$BeginDate-$EndDate/

$WebResponse | foreach-object {[datetime]::parseexact(($WebResponse.occurred).Trim(), 'yyyy-MM-ddTHH:mm:ss',$null).ToString('yyyy-MM-dd')} | Export-CSV $File -NoTypeInformation

And this is the error i get

Exception calling "ParseExact" with "3" argument(s): "String was not recognized as a valid DateTime."
At C:\Users\bellc\Desktop\test.ps1:12 char:33
+ ... ach-object {[datetime]::parseexact(($WebResponse.occurred).Trim(), 'y ...
+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : FormatException
0 Answers
Related