我有一个字符串形式的日期和输入时间格式(例如:hh:mm tt)。我想将日期转换为 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.TryParseExact
最佳答案
您正在寻找 DateTime
类型:
DateTime.Parse("01/02/11").ToString("hh:mm tt")
关于c# - 将日期转换为日期时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6587486/