We help IT Professionals succeed at work.

How do I convert this particular string to datetime using VB.net

Given the string, "Wed, 12 May 2010 13:05:04 -0400 (EDT)", how can I convert it to a long datetime format (mm/dd/yyyy hh:mm:ss xM)?

Thanks!
Comment
Watch Question

Top Expert 2015

Commented:
Not sure what you wanted at the end with "xM".

Dim dt2 As DateTime = New DateTime(2010, 5, 12, 13, 5, 4)
MsgBox(dt2.ToString("mm/dd/yyyy hh:mm:ss"))

Author

Commented:
xM was AM/PM.  Is there a better way than having to parse out the string and converting May to 5, etc?
Top Expert 2015
Commented:
You can do this and it will convert the string to a time.

Dim dateStr As String = "Wed, 12 May 2010 13:05:04 -0500 GMT"
Dim d3 As Date = Date.Parse(dateStr)        
MsgBox(d3.ToString("mm/dd/yyyy hh:mm:ss tt '(EDT)'"))

But having (EDT) in the string will not parse. If the time in the string represents GMT time you can specify how many hours difference between GMT and the time zone you want (as in the example above) and it will give you the correct time for that new zone. Eastern Daylight Time is 5 hours less than GMT time so the above string parses to EDT time.

Author

Commented:
Thanks so much, exactly what i needed.  Good explanation