0
dim dt as string= "03/22/20 20:12:27.320"

Dim tempDate As DateTime = DateTime.ParseExact(dt, "MM/dd/yy hh:mm:ss.fff", CultureInfo.GetCultureInfo("en-US").DateTimeFormat)

This gives error sometimes : System.FormatException: 'String was not recognized as a valid DateTime.'

Why? It seems to be formatted properly. VB.net 4.6.1 framework

2 Answers 2

1

"hh" is 12-hour format. 20 is obviously not a valid hour in 12-hour format. If you want 24-hour format then use "HH".

Sign up to request clarification or add additional context in comments.

Comments

0

Have you tried to use CDate() function?

dim dt as string= "03/22/20 20:12:27.320"
dim tempDate as Datetime = Cdate(dt)

or

dim tempDate as Datetime = Cdate("03/22/20 20:12:27.320")

1 Comment

That would only work when the system culture is set to US. Everywhere else places the day before the month and so the use of CDate would only work correctly for 12 days of the year. The rest of the time it would either fail or, even worse, succeed but produce the wrong value.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.