C# 日期时间对象转换

C# 日期时间对象转换,c#,C#,我有下面的C#在尝试将字符串解析为datetime时出现上述错误 DateTime currDate = DateTime.MinValue; DateTime.TryParseExact(date.Trim(), "M/d/yyyy", null, System.Globalization.DateTimeStyles.None, out currDate); if (currDate == DateTime.MinValue) currDate = Convert.ToDateTim

我有下面的C#在尝试将字符串解析为datetime时出现上述错误

DateTime currDate = DateTime.MinValue;
DateTime.TryParseExact(date.Trim(), "M/d/yyyy", null, System.Globalization.DateTimeStyles.None, out currDate);
if (currDate == DateTime.MinValue)
    currDate = Convert.ToDateTime(date.Trim());
image flow label.text看起来像这样
{1/1/0001 12:00:00 AM}


关于如何转换此格式有什么想法吗?

您应该拥有2015年3月31日
的格式
d MMM yyy

DateTime.TryParseExact(date.Trim(), 
                       "d MMM yyyy", 
                       none, 
                       DateTimeStyles.None, 
                       out currDate);

您需要使用
MMMM
格式指定MSDN文档中指定的完整月份

你的代码应该是这样的

using System;

public class Program
{
    public static void Main()
    {
        DateTime currDate = DateTime.MinValue;
        DateTime.TryParseExact("31 March 2015", "dd MMMM yyyy", null, System.Globalization.DateTimeStyles.None, out currDate);
        Console.WriteLine(currDate);
    }
}

正在使用的是网络提琴。

日期修剪()的值是多少?与
label.text
?date.Trim()的值是2015年3月31日与label.textImage flow label.text没有关系,我的意思是CurrDate的值我尝试了所有的解决方案,但得到了相同的错误“字符串未被识别为有效的日期时间。索引1处有一个未知单词。”让我向所有人说明,在我的代码中DateTime currDate=DateTime.MinValue;DateTime.TryParseExact(date.Trim(),“MM/dd/yyyy”,null,System.Globalization.DateTimeStyles.None,out currDate);如果(currDate==DateTime.MinValue)currDate=Convert.ToDateTime(date.Trim());date的值为“2015年3月31日”,currDate的值为“1/1/0001 12:00:00 AM”。谢谢大家。我不能指定“2015年3月31日”、“d MMMM YYYY”,因为每次文档都不能包含相同的日期