0

How to convert following String to DateTime in VB?
I got error when executing code as below:

Dim date As String = "Wed Mar 30 2016 00:00:00 GMT+0800 (Malay Peninsula Standard Time)"
DateTime.Parse(date)

System.FormatException: "String was not recognized as a valid DateTime."

3
  • The date you have given seems pretty confusing.. Commented Apr 18, 2016 at 12:18
  • i got it using <input type="date"> Commented Apr 18, 2016 at 12:19
  • Have you checked this answer : stackoverflow.com/questions/12675421/… ? as suggested, can you directly pass the timestamp ? Commented Apr 18, 2016 at 12:31

1 Answer 1

3

Your string is clearly not a standard date and time format for any culture.

But since your string has UTC Offset, I would parse it to DateTimeOffset instead. And since it does not keep timezone name and it's abbreviations, you need to escape them as a string literal delimiter. You can use ddd MMM dd yyyy HH:mm:ss 'GMT'zzz '(Malay Peninsula Standard Time)' format for that.

C#

var date = "Wed Mar 30 2016 00:00:00 GMT+0800 (Malay Peninsula Standard Time)";
var dto = DateTimeOffset.ParseExact(date, 
              "ddd MMM dd yyyy HH:mm:ss 'GMT'zzz '(Malay Peninsula Standard Time)'",
              CultureInfo.InvariantCulture);

VB.NET

Dim [date] = "Wed Mar 30 2016 00:00:00 GMT+0800 (Malay Peninsula Standard Time)"
Dim dto = DateTimeOffset.ParseExact([date], "ddd MMM dd yyyy HH:mm:ss 'GMT'zzz '(Malay Peninsula Standard Time)'", CultureInfo.InvariantCulture)

Now you have a DateTimeOffset as {30.03.2016 00:00:00 +08:00}. Now you can use it's .DateTime ot .UtcDateTime properties as you want.

enter image description here

Sign up to request clarification or add additional context in comments.

1 Comment

Ups, c# tag removed. Since I'm not familiar with vb.net syntax, I converted this code to vb.net with converter.telerik.com

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.