将日期转换为日期时间

时间:2011-07-05 18:56:24

标签: c# asp.net datetime date

我有一个字符串形式的日期和输入timeformat(例如:hh:mm tt)。我想将date转换为dateime字符串。以下是我的代码:

    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.TryParseExact

2 个答案:

答案 0 :(得分:4)

您正在寻找DateTime类型:

DateTime.Parse("01/02/11").ToString("hh:mm tt")

答案 1 :(得分:1)

DateTime.ParseExact('your date string', format, culture)

我错过了什么吗?

相关问题