Reputation: 453
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?
Upvotes: 0
Views: 420
Reputation: 21905
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)
Upvotes: 1
Reputation: 1618
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.
Upvotes: 1