如何使用DateTime.ParseExact解析此内容?

时间:2018-12-14 20:36:14

标签: c# .net

我一直在阅读如何使用DateTime.ParseExact,但是我似乎无法弄清楚如何解析它:

2018年12月16日世界标准时间(UTC)

这是我目前正在做的事情:

DateTime.ParseExact(launch.windowstart, 
      "MMMM dd, yyyy H:mm:ss \\G\\M\\Tzzz", 
      System.Globalization.CultureInfo.InvariantCulture)

2 个答案:

答案 0 :(得分:1)

看起来您只需要稍微修改一下格式字符串即可。我不确定结尾是什么意思,但是正如Broots在评论中提到的那样,您需要准确设置格式。看来您只需要修改小时和时区捕获(在本例中为UTC):

DateTime.ParseExact(launch.windowstart, 
  "MMMM dd, yyyy HH:mm:ss UTC", 
  System.Globalization.CultureInfo.InvariantCulture);

这应该返回我认为您正在寻找的DateTime对象。

答案 1 :(得分:0)

我不得不编写一个解析器,但这是它的代码。

public static DateTime LLTimeToDateTime(string launchLibraryTime)
    {
        string[] segTime = launchLibraryTime.Split(' ');

        int monthLength = segTime[0].Length;

        int dayLength = segTime[1].Length - 1;

        string dateTimeFormatter = "";

        for (int i = 0; i < monthLength; i++)
        {
            dateTimeFormatter += "M";
        }

        dateTimeFormatter += " ";


        for (int i = 0; i < dayLength; i++)
        {
            dateTimeFormatter += "d";
        }

        dateTimeFormatter += ", yyyy HH:mm:ss";

        launchLibraryTime = launchLibraryTime.Substring(0, launchLibraryTime.Length - 4);

        return DateTime.ParseExact(launchLibraryTime, dateTimeFormatter,
            System.Globalization.CultureInfo.InvariantCulture);
    }
相关问题