[英]How to Parse a Date Time with TimeZone Info
I have following string ,我有以下字符串,
Thu Sep 24 2015 00:00:00 GMT+0530 (IST) 2015 年 9 月 24 日星期四 00:00:00 GMT+0530 (IST)
I tried with following but it's faling.我试过跟随,但它正在失败。
var twDate = DateTime.Parse("Thu Sep 24 2015 00:00:00 GMT+0530 (IST) ");
Can not use replace , as IST wont be fixed.不能使用 replace ,因为 IST 不会被修复。 Any Ideas?有任何想法吗?
You need to trim the time zone abbreviation off using normal string operations, then specify a custom date and time format string.您需要使用普通字符串操作修剪时区缩写,然后指定自定义日期和时间格式字符串。 For example:例如:
// After trimming
string text = "Thu Sep 24 2015 00:00:00 GMT+0530";
var dto = DateTimeOffset.ParseExact(
text,
"ddd MMM d yyyy HH:mm:ss 'GMT'zzz",
CultureInfo.InvariantCulture);
Console.WriteLine(dto);
Note the use of CultureInfo.InvariantCulture
here - you almost certainly don't want to parse using the current thread's current culture.注意使用的CultureInfo.InvariantCulture
在这里-你几乎肯定不希望使用当前线程的当前文化解析。
If your string always has the same format and length, you could use this to get UTC:如果您的字符串始终具有相同的格式和长度,您可以使用它来获取 UTC:
String dtstr = "Thu Sep 24 2015 00:00:00 GMT+0530 (IST)";
int zoneMin = int.Parse(dtstr.Substring(29, 2)) * 60 + int.Parse(dtstr.Substring(31, 2));
if (dtstr.Substring(28, 1) == "+") zoneMin = -zoneMin;
DateTime dt = DateTime.Parse(dtstr.Substring(0, 24)).AddMinutes(zoneMin);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.