String was not recognized as a valid DateTime

The error shown in question might have been thrown by Convert.ToDateTime method as shown below:
string dateString = "20/08/2013";
//The following code throws
//FormatException: String was not recognized as a valid DateTime
DateTime date = Convert.ToDateTime(dateString);
 
 
In the above code the dateString represents Date in the Day/Month/Year format.
By default the en-US culture is used by .NET according to which the Date is in Month/Day/Year format.

So, when ToDateTime method is used it throws error as 20 is out of Month range.

To avoid this error the appropriate culture can be used as follows
the parsing fails, but it will not throw error, rather it returns false indicating that the parsing failed.

Comments

Popular posts from this blog

Performance Optimization in Sitecore

𝗙𝗹𝘂𝗲𝗻𝘁𝗩𝗮𝗹𝗶𝗱𝗮𝘁𝗶𝗼𝗻 𝗶𝗻 𝗔𝗦𝗣.𝗡𝗘𝗧 𝗖𝗼𝗿𝗲 - 𝗖𝗹𝗲𝗮𝗻, 𝗙𝗹𝗲𝘅𝗶𝗯𝗹𝗲 𝗠𝗼𝗱𝗲𝗹 𝗩𝗮𝗹𝗶𝗱𝗮𝘁𝗶𝗼𝗻 𝗳𝗼𝗿 𝗠𝗼𝗱𝗲𝗿𝗻 .𝗡𝗘𝗧 𝗔𝗽𝗽𝘀

Azure Event Grid Sample code