Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert string to datetime in vb.net

I have a datetime that looks like this:

201210120956
ccyyMMDDhhmm

When I try this:

Dim convertedDate As Date = Date.Parse(DateString)
Return convertedDate

I get back this:

#10/12/2012#

I'm losing the time on it.

I've read this convert string to datetime vb.net but when I use datetime.ParseExact() I get:

cannot resolve symbol 'ParseExact'

Is there a way to convert this to a date time without using substring? A straight conversion?

like image 394
ErocM Avatar asked Oct 12 '12 15:10

ErocM


2 Answers

Pass the decode pattern to ParseExact

Dim d as string = "201210120956"
Dim dt = DateTime.ParseExact(d, "yyyyMMddhhmm", Nothing)

ParseExact is available only from Net FrameWork 2.0.
If you are still on 1.1 you could use Parse, but you need to provide the IFormatProvider adequate to your string

like image 71
Steve Avatar answered Oct 17 '22 02:10

Steve


You can try with ParseExact method

Sample

Dim format As String  
format = "d" 
Dim provider As CultureInfo = CultureInfo.InvariantCulture
result = Date.ParseExact(DateString, format, provider)
like image 1
Aghilas Yakoub Avatar answered Oct 17 '22 03:10

Aghilas Yakoub