Rocky Singh
Rocky Singh

Reputation: 15430

Converting date to datetime

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

Upvotes: 1

Views: 7378

Answers (2)

hungryMind
hungryMind

Reputation: 6999

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

Am I missing something?

Upvotes: 1

SLaks
SLaks

Reputation: 887355

You're looking for the DateTime type:

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

Upvotes: 4

Related Questions