在 vb.net 中将字符串转换为日期时间

问题描述:

我有一个如下所示的日期时间:

I have a datetime that looks like this:

201210120956
ccyyMMDDhhmm

当我尝试这个时:

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

我回来了:

#10/12/2012#

我正在浪费时间.

我读过这个将字符串转换为日期时间 vb.net 但是当我使用 datetime.ParseExact() 我得到:

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

无法解析符号ParseExact"

cannot resolve symbol 'ParseExact'

有没有办法在不使用子字符串的情况下将其转换为日期时间?直接转换?

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

将解码模式传递给 ParseExact

Pass the decode pattern to ParseExact

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

ParseExact 可从 Net FrameWork 2.0 获得.
如果您仍然使用 1.1,您可以使用 Parse,但您需要提供适合您的字符串的 IFormatProvider

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