-1

这个问题看起来不太明显,但我几乎没有找到这方面的建议。

我有一个日期和时间作为字符串,如下所示:

string s = "Fri Jun 28 2013 00:00:00 GMT+0530 (India Standard Time)";
DateTime from = Convert.ToDateTime(s).ToUniversalTime();

但这会引发异常:

{System.SystemException}: {"String was not recognized as a valid DateTime."}

有人可以建议我该如何处理吗?

4

1 回答 1

4

您可以使用以下TryParseExact方法:

string s = "Fri Jun 28 2013 00:00:00 GMT+0530 (India Standard Time)";
DateTime date;
if (DateTime.TryParseExact(s, "ddd MMM dd yyyy hh:mm:ss 'GMT'zzz '(India Standard Time)'", CultureInfo.InvariantCulture, DateTimeStyles.None, out date))
{
    // You could use the date here
}
else
{
    // The string was not in the correct format
}
于 2013-06-27T10:52:11.853 回答