Convert string to datetime in vb.net

后端 未结 3 438
温柔的废话
温柔的废话 2020-12-03 15:25

I have a datetime that looks like this:

201210120956
ccyyMMDDhhmm

When I try this:

Dim convertedDate As Date = Date.Parse(D         


        
相关标签:
3条回答
  • 2020-12-03 15:59

    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)
    
    0 讨论(0)
  • 2020-12-03 16:14

    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

    0 讨论(0)
  • 2020-12-03 16:14

    As an alternative, if you put a space between the date and time, DateTime.Parse will recognize the format for you. That's about as simple as you can get it. (If ParseExact was still not being recognized)

    0 讨论(0)
提交回复
热议问题