Exception calling "ParseExact" with "3" argument(s): "String was not recognized as a valid DateTime."

Juan Pazo 21 Reputation points
2022-08-02T11:49:04.117+00:00

We have been dealing with a runbook for a month that stopped working.
7th of July it was the last time that it worked.

$time = [datetime]::parseexact($RGTAG_Expiration,'YYYY-MM-DD', $null).ToString('MM/dd/yyyy')
$countdays = (New-TimeSpan -start (get-date -format MM/dd/yyyy) -end $time)

These are the key variables.

I've reviewed the documentation at:
https://learn.microsoft.com/en-us/dotnet/api/system.datetime.parseexact?view=net-6.0
https://learn.microsoft.com/en-us/rest/api/storageservices/formatting-datetime-values

So I tried changing the format on my variable as per the following:

The following ISO 8601 UTC formats are currently accepted by Azure Storage. The date value is required, while the time value is optional:
YYYY-MM-DD

But at the end of the day, the error comes from the "parseexact($RGTAG_Expiration,'YYYY-MM-DD', $null)" section. I haven't seen any updates on modules that could have impacted the runbook exactly from the 7th of July.

Any ideas would be really appreciated.

Azure Automation
Azure Automation
An Azure service that is used to automate, configure, and install updates across hybrid environments.
1,366 questions
0 comments No comments
{count} votes

Accepted answer
  1. AnuragSingh-MSFT 21,546 Reputation points Moderator
    2022-08-03T07:40:50.453+00:00

    Hi @Juan Pazo ,

    Thank you for reaching out to Microsoft Q&A for this question.

    The issue is related to second parameter of [datetime]::parseexact - i.e., the format string. Note that PowerShell uses the .NET assembliess and conventions. In .NET, date and year are represented by lower-case letters ( dd and yyyy respectively). Therefore, the $time variable should be assigned as:

    $time = [datetime]::parseexact($RGTAG_Expiration,'yyyy-MM-dd', $null).ToString('MM/dd/yyyy')  
    

    For details on DateTime format string in .NET, please see Custom date and time format strings

    I believe that you are rereferring to the following Azure Doc, related to format of DateTime for Azure Storage. This article specifies the guidelines to be used for DateTime format in Azure Storage. The mentioned letters are provided for generic understanding (which is independent of programming/scripting language/framework). That format will have to be accordingly translated, depending on the programming/scripting language being used.
    Formatting DateTime values in Azure Storage

    Please let me know if you have any questions.

    ---
    Please 'Accept as answer' if it helped so that it can help others in the community looking for help on similar topics.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.