Hi
On below code i am getting above error. Value in sessiondate is 27/12/2021 00:00:00
DateTime sessionDate = DateTime.ParseExact(colum1.SessionDate.ToString(), "dd-MM-yyyy", CultureInfo.InvariantCulture);
Thanks
Hi
On below code i am getting above error. Value in sessiondate is 27/12/2021 00:00:00
DateTime sessionDate = DateTime.ParseExact(colum1.SessionDate.ToString(), "dd-MM-yyyy", CultureInfo.InvariantCulture);
Thanks
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Jay PankhaniyaRank #798 · 2.9K reputationPosted Apr 22, 2023, 4:50 AM EDT
The error message "String was not recognized as a valid DateTime format" occurs when the format of the string being parsed does not match the format string passed to
ParseExact()method.In your case, you are trying to parse a string with the format "27/12/2021 00:00:00" using the format string "dd-MM-yyyy", which is causing the error.
To fix this error, you need to pass the correct format string that matches the format of the input string. In your case, you can use the format string "dd/MM/yyyy HH:mm:ss" to parse the string correctly. Here is an example of how you can modify your code:
DateTime sessionDate = DateTime.ParseExact(colum1.SessionDate.ToString(), "dd/MM/yyyy HH:mm:ss", CultureInfo.InvariantCulture);This code should be able to parse the input string "27/12/2021 00:00:00" correctly and assign the parsed
DateTimevalue to thesessionDatevariable.Make sure to adjust the format string to match the actual format of the string you are trying to parse.
Amit MohantyRank #112 · 20K reputationPosted Apr 24, 2023, 12:23 AM EDT
The issue is that the format provided in the
ParseExactmethod does not match the format of the stringcolum1.SessionDate.ToString(). Specifically, the format provided is "dd-MM-yyyy" but the actual format of the string appears to be "dd/MM/yyyy HH:mm:ss". Therefore, theParseExactmethod is unable to parse the string into aDateTimeobject.DateTime sessionDate = DateTime.ParseExact(colum1.SessionDate.ToString(), "dd/MM/yyyy HH:mm:ss", CultureInfo.InvariantCulture);Vishal YelveRank #178 · 13.6K reputationPosted Apr 24, 2023, 1:23 AM EDT
Hi Ramco,
Try this
DateTime sessionDate = DateTime.ParseExact(colum1.SessionDate.ToString(), "dd/MM/yyyy", CultureInfo.InvariantCulture); OR DateTime sessionDate = DateTime.ParseExact(colum1.SessionDate.ToString().trim(), "dd/MM/yyyy", CultureInfo.InvariantCulture);Tuhin PaulRank #7 · 71.9K reputationPosted Apr 24, 2023, 3:00 AM EDT
you can simply convert it to a string in the desired format using the ToString method and pass that string directly to DateTime.ParseExact
DateTime sessionDate = DateTime.ParseExact(colum1.SessionDate.ToString("dd-MM-yyyy"), "dd-MM-yyyy", CultureInfo.InvariantCulture);The ToString("dd-MM-yyyy") method call formats the DateTime value as a string in the "dd-MM-yyyy" format before passing it to DateTime.ParseExact.
Tuhin PaulRank #7 · 71.9K reputationPosted Apr 24, 2023, 3:02 AM EDT
The code in my previous answer will convert the
colum1.SessionDatevalue to a string in the "dd-MM-yyyy" format using theToString("dd-MM-yyyy")method and then parses it back to aDateTimeobject usingDateTime.ParseExactwith the same format string. This ensures that theDateTimeobject is correctly parsed from the string representation with the desired format.