0

I have date as string in the format 16 Oct 2015 12:05:29:000 and I have it in DateString variable.

I am using the following line of code to convert it into date format.

Dim FormattedDate As Date = Date.ParseExact(DateString, "dd mmm yyyy HH:mm:ss.FFF", System.Globalization.CultureInfo.InvariantCulture)

I am getting the following error. String was not recognized as a valid DateTime

what am I doing wrong?

0

2 Answers 2

1

Your code is almost correct and just need to fix two things in existing format string ie. dd mmm yyyy HH:mm:ss.FFF

1.) dd mmm yyyy HH:mm:ss.FFF - here the format for Month name should be MMM because you want get Oct(16 Oct 2015 12:05:29:000)

2.) 12:05:29:000 is the time in your string but you're trying to parse it as HH:mm:ss.FFF <- (note : :000 to .FFF will yields the error

and the final code should be

You've to use dd MMM yyyy HH:mm:ss:FFF instead of dd mmm yyyy HH:mm:ss.FFF

Dim FormattedDate As Date = Date.ParseExact(dateString, _
                                                    "dd MMM yyyy HH:mm:ss:FFF", _
                                                    System.Globalization.CultureInfo.InvariantCulture)

DEMO

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

Comments

1

try with this

Dim datestring = "16 Oct 2015 12:05:29.000"
Dim FormattedDate As Date = Format(CDate(datestring), "dd MMM yyyy HH:mm:ss.FFF")

or as the same way you are trying

Dim datestring = "16 Oct 2015 12:05:29.000"
Dim FormattedDate As Date = Date.ParseExact(datestring, "dd MMM yyyy HH:mm:ss.FFF", System.Globalization.CultureInfo.InvariantCulture)

you were using mmm instead of MMM for Three-letter month.please have a close look at the date string and format u specified.there is a difference which caused string not recogonized problem.

Comments

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.