C# 将日期转换为日期时间

C# 将日期转换为日期时间,c#,asp.net,datetime,date,C#,Asp.net,Datetime,Date,我有一个字符串形式的日期,并输入timeformatex:hh:mmtt。我想将日期转换为日期输入法字符串。下面是我的代码: string inputDate = "01/02/11"; string inputTimeFormat = "hh:mm tt"; Regex rgx1 = new Regex("tt"); string time1 = rgx1.Replace(inputTimeFormat, "AM"); Regex rgx = new

我有一个字符串形式的日期,并输入timeformatex:hh:mmtt。我想将日期转换为日期输入法字符串。下面是我的代码:

    string inputDate = "01/02/11";
    string inputTimeFormat = "hh:mm tt";

    Regex rgx1 = new Regex("tt");
    string time1 = rgx1.Replace(inputTimeFormat, "AM");
    Regex rgx = new Regex("[^ :AM]");
    string time = rgx.Replace(time1, "0");
    string dateTime = string.Format("{0} {1}", inputDate, time);
    //output: 01/02/11 00:00 AM
现在它以datetime字符串格式提供输出,有没有更好的方法

编辑:我需要字符串格式的datetime,在此之后我可以应用datetime。TryParseExact

您正在查找datetime类型:

您正在查找日期时间类型:

我错过什么了吗


我遗漏了什么吗?

不过,英国的分析方式与美国不同。这可能是他想要的,也可能是他想要的。ParseExact还带有一个明确的日期格式字符串。这将给出时间而不是日期时间。@Rocky:然后您需要更改格式字符串。例如,yyyy-MM-dd-hh:MM-tt这在英国的解析方式与在美国不同。这可能是他想要的,也可能是他想要的。ParseExact还带有一个明确的日期格式字符串。这将给出时间而不是日期时间。@Rocky:然后您需要更改格式字符串。例如,yyyy MM dd hh:MM ttParse不使用格式作为第二个参数,但IFormatProviderOops抱歉,我的意思是ParseExactParse不使用格式作为第二个参数,但IFormatProviderOops抱歉,我的意思是ParseExactYour的编辑不正确。您可以立即调用TryParseExact。您的编辑不正确。你现在可以打电话给TryParseExact。
DateTime.Parse("01/02/11").ToString("hh:mm tt")
DateTime.ParseExact('your date string', format, culture)