正在将日期转换为日期时间

本文关键字:日期 时间 转换 | 更新日期: 2023-09-27 18:01:07

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

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

正在将日期转换为日期时间

您正在寻找DateTime类型:

DateTime.Parse("01/02/11").ToString("hh:mm tt")
DateTime.ParseExact('your date string', format, culture)

我是不是错过了什么?