将字符串Fri Jul 11 00:00:00 UTC + 0800 2014转换为vb.net mvc 5中的datetime
问题描述:
嗨先生/ s,
美好的一天。
我很难过找到这个日期字符串到日期时间的转换
在vb.net中mvc 5
这是我的样本字符串日期:
Hi Sir/s,
Good day.
I'm having a hard time on finding conversion of this date string to date time
in vb.net mvc 5
Here's my sample string date:
Fri Jul 11 00:00:00 UTC+0800 2014
我想转换这个字符串到日期时间值
我做了这个解决方案,但我仍然收到这个错误:
I would like to convert this string to datetime value
I made this solution but still I'm getting this error:
String was not recognized as a valid DateTime.
这是我在vb.net中的现有代码
Here's my existing code in vb.net
'Dim datestring As [String] = "Fri Jul 11 00:00:00 UTC+0800 2014"
Dim datestring As [String] = descriptor.Value.ToString
Dim d = datestring.Split(New Char() {" "c}, 5).Take(4)
Dim dt As DateTime = DateTime.Parse(d.Aggregate(Function(x, y) x + " " + y))
descriptor.Value = dt
我希望有人可以帮助我。提前谢谢...
I hope someone could help me. Thank you in advance...
答
尝试使用DateTime.TryParseExact:
Try using DateTime.TryParseExact:
Dim [date] As String = "Fri Jul 11 00:00:00 UTC+0800 2014"
Dim dat As DateTime
If DateTime.TryParseExact([date], "ddd MMM dd HH:mm:ss 'UTC'zzz yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, dat) Then
Console.WriteLine(dat)
End If
按字符串化UTC指标并添加zzzUTC-plus-offset代码做你想做的事。
By "stringifying" the UTC indicator and adding the "zzz" UTC-plus-offset code it shoudl do what you want.