-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
  • 4,572
  • 3
  • 25
  • 39
user979033
  • 4,180
  • 5
  • 28
  • 43
  • 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,387
  • 1
  • 8
  • 12
  • 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