开发者

Converting date to datetime

开发者 https://www.devze.com 2023-03-17 06:07 出处:网络
I have a date in string form and input timeformat(ex: hh:mm tt). I want to convert date to dateime string. Below is my code :

I have a date in string form and input timeformat(ex: hh:mm tt). I want to convert date to dateime string. Below is my code :

    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

Right now it is giving output in datetime string format, Is there any better way of doing the same?

EDIT: I need datetime in string format here and after that I can apply Datetime.TryParseExact


You're looking for the DateTime type:

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


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

Am I missing something?

0

精彩评论

暂无评论...
验证码 换一张
取 消