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.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Tuhin PaulPosted Apr 24, 2023, 7:02 AM
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.Tuhin PaulPosted Apr 24, 2023, 7:00 AM
you can simply convert it to a string in the desired format using the ToString method and pass that string directly to DateTime.ParseExact
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.
Vishal YelvePosted Apr 24, 2023, 5:23 AM
Hi Ramco,
Try this
Amit MohantyPosted Apr 24, 2023, 4:23 AM
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.Jay PankhaniyaPosted Apr 22, 2023, 8:50 AM
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:
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.