Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何以这种格式解析DateTime?“2010年5月23日星期日22:00:00 UTC+0300”_C#_Datetime - Fatal编程技术网

C# 如何以这种格式解析DateTime?“2010年5月23日星期日22:00:00 UTC+0300”

C# 如何以这种格式解析DateTime?“2010年5月23日星期日22:00:00 UTC+0300”,c#,datetime,C#,Datetime,这是一个快速的例子,我想解析一个以这种格式出现的日期Sun May 23 22:00:00 UTC+0300 2010 这是有效的UTC日期时间吗?如何解析它?我试过: 然而,这不起作用,感谢任何帮助 对不起,我之前的回答太简单了。 在您的日期格式中将MM替换为MMM,应该可以。很抱歉我之前的回答过于简单。 在日期格式中将MM替换为MMM,这样就可以了。这不是标准的.NET格式,因此您可能需要手动解析它。UTC+0300位表示时区,其他所有内容都是日期和时间的一部分。这不是标准的.NET格式,因

这是一个快速的例子,我想解析一个以这种格式出现的日期Sun May 23 22:00:00 UTC+0300 2010 这是有效的UTC日期时间吗?如何解析它?我试过:


然而,这不起作用,感谢任何帮助

对不起,我之前的回答太简单了。
在您的日期格式中将MM替换为MMM,应该可以。

很抱歉我之前的回答过于简单。
在日期格式中将MM替换为MMM,这样就可以了。

这不是标准的.NET格式,因此您可能需要手动解析它。UTC+0300位表示时区,其他所有内容都是日期和时间的一部分。

这不是标准的.NET格式,因此您可能需要手动解析它。UTC+0300位表示时区,其他所有内容都是日期和时间的一部分。

它不是标准格式,但您仍然可以解析它

DateTime dt = DateTime.ParseExact(s,"ddd MMM dd HH:mm:ss UTCzzzz yyyy", System.Globalization.CultureInfo.InvariantCulture);
        string format = "ddd mmm dd HH:mm:ss zzzzz yyyy";
        string temp = "Sun May 23 22:00:00 UTC+0300 2010";
        DateTime time = DateTime.ParseExact(temp, format, CultureInfo.InvariantCulture);

它不是标准格式,但您仍然可以解析它

        string format = "ddd mmm dd HH:mm:ss zzzzz yyyy";
        string temp = "Sun May 23 22:00:00 UTC+0300 2010";
        DateTime time = DateTime.ParseExact(temp, format, CultureInfo.InvariantCulture);

从给出的示例中,无法判断月份是以1月、2月、5月等3个字母的形式,还是以完整的1月、2月、5月等形式

如果应为简短形式,请使用:

::UTC

如果格式较长,请使用:

::UTC


可用格式说明符的详细信息可在

中找到。从给出的示例中,无法判断月份应采用3个字母的格式一月、二月、五月等,还是完整的格式一月、二月、五月等

如果应为简短形式,请使用:

::UTC

如果格式较长,请使用:

::UTC


有关可用格式说明符的详细信息,请访问

我尝试了@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);
}

我尝试了@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);
}

当然,我的意思是,当然,但问题是自定义格式。DateTime.ParseSSun 2010年5月23日22:00:00 UTC+0300;将通过一个格式化异常。所以当然,我的意思是,当然,但问题是自定义格式。DateTime.ParseSSun 2010年5月23日22:00:00 UTC+0300;将通过一个格式化异常。所以令人惊叹的工作得很有魅力。zzzz以正确的方式表示时区。在我的尝试中,我将时区翻译为UTC+0300,如果应用程序部署到另一个时区不同的地方,它肯定会中断。谢谢johncatfish!令人惊叹的工作得很有魅力。zzzz以正确的方式表示时区。在我的尝试中,我将时区翻译为UTC+0300,如果应用程序部署到另一个时区不同的地方,它肯定会中断。谢谢johncatfish!