-4

So I have this `string:

string time = "20201006 10:42:42.925"

Which look like valid time format.

And this is what I have try:

DateTime dt1 = DateTime.Parse(time);

And got this error: String was not recognized as a valid DateTime.

Md. Suman Kabir
  • 5,243
  • 5
  • 25
  • 43
user979033
  • 5,430
  • 7
  • 32
  • 50
  • You have asked over a 100 questions, please read [ask] and show that you at least pasted that error message in a web search engine and have attempted to solve this yourself. `DateTime.Parse()` accepts a format related to the current culture, but not this particular nonstandard format, so you'll have to provide the format to `(Try)ParseExact()`. – CodeCaster Oct 06 '20 at 07:48

1 Answers1

-2

Use TryParse Method, eg

dateTime dt1;
if DateTime.TryParse(time, out dt1)
{
// all good

} else{
// no good
}
Programnik
  • 1,449
  • 1
  • 9
  • 13
  • Assuming the OP is interested in the actual parsed DateTime value, ending up in the `else` will do them no good. – CodeCaster Oct 06 '20 at 07:50