Parsing string “Sat, 14 Jan 2017 12:12:12 Europe/Warsaw” to DateTime
我需要解析字符串
Sat, 14 Jan 2017 12:12:12 Europe/Warsaw
到
我试过了:
1 2 | var datestring ="Sat, 14 Jan 2017 12:12:12 Europe/Warsaw"; DateTime.TryParse(datestring, out expDt); |
但它不起作用。
在此先感谢您的帮助。
我想你可以这样做:
1 2 3 4 5 6 7 8 | string datestring ="Sat, 14 Jan 2017 12:12:12 Europe/Warsaw"; // remove"Europe/Warsaw" because it wont be used. datestring = datestring.Substring(0, datestring.LastIndexOf(' ')); // now datestring looks like"Sat, 14 Jan 2017 12:12:12" // so you should adapt the format: string dateFormat ="ddd, dd MMM yyyy HH:mm:ss"; // now you can use DateTime.ParseExact to retrieve DateTime object DateTime dt = DateTime.ParseExact(datestring, dateFormat, System.Globalization.CultureInfo.InvariantCulture); |
这应该解析为纠正
调用
编辑:
对于其他假设
这就是为什么对我来说从字符串中提取TimeZone是没用的。 因为它对
1 2 3 4 5 6 7 8 9 10 11 12 13 | string datestring ="Sat, 14 Jan 2017 12:12:12 Europe/Warsaw"; // remove"Europe/Warsaw" because it wont be used. string datestr = new string(datestring.Take(datestring.LastIndexOf(' '))); // now datestring looks like"Sat, 14 Jan 2017 12:12:12" // so you should adapt the format: string dateFormat ="ddd, dd MMM yyyy HH:mm:ss"; // now you can use DateTime.ParseExact to retrieve DateTime object DateTime dt = DateTime.ParseExact(datestring, dateFormat, System.Globalization.CultureInfo.InvariantCulture); string timezonestr = new string(datestring.Skip(datestring.LastIndexOf(' ') + 1)); try { TimeZoneInfo timzeone = TimeZoneInfo.FindSystemTimeZoneById(timezonestr); } catch { /* probably an error because there's no timezone called Europe/Warsaw */ } |