找到我试图通过@johncatfish提出的解决方案,它做什么,我的期望。我会假设你确实想保留时区信息。
[Test()]
public void TestCaseWorks()
{
string format = "ddd MMM dd HH:mm:ss UTCzzzzz yyyy";
string temp = "Sun May 23 22:00:00 UTC+0300 2010";
DateTime time = DateTime.ParseExact(temp, format, CultureInfo.InvariantCulture);
Assert.AreEqual(DayOfWeek.Sunday, time.DayOfWeek);
Assert.AreEqual(5, time.Month);
Assert.AreEqual(23, time.Day);
Assert.AreEqual(0, time.Minute);
Assert.AreEqual(0, time.Second);
Assert.AreEqual(2010, time.Year);
// Below is the only actually useful assert -- making sure the
// timezone was parsed correctly.
// In my case, I am GMT-0700, the target time is GMT+0300 so
// 22 + (-7 - +3) = 12 is the expected answer. It is an exercise
// for the reader to make a robust test that will work in any
// timezone ;).
Assert.AreEqual(12, time.Hour);
}
太棒了!像魅力一样工作。以正确方式指示时区的zzzz。在我的尝试中,我将时区字面翻译为“UTC + 0300”,如果将应用程序部署到具有不同时区的另一个地方,该时区肯定会中断。谢谢johncatfish! – Galilyou 2010-05-24 10:31:53