Reputation: 145
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."
Upvotes: 0
Views: 2447
Reputation: 98740
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.
Upvotes: 3