Home > DeveloperSection > Forums > Convert date in string to DateTime with same format
Goti Bandu

Total Post:119

Posted on    May-10-2013 2:00 AM

 C# C# 

 1 Reply(s)
 1013  View(s)
Rate this:
Hi Guys!

I have a string that has a date stored in it.

String date = "03-05-2013 00:00:00";
I parsed it to Datetime as follows:

DateTime Start = DateTime.Parse(date);
Start.ToString() gave me "3/5/2013 12:0:00 AM"

I also used:

DateTime Start = DateTime.ParseExact(date,"dd-MM-yyyy HH:mm:ss",CultureInfo.InvariantCulture);
Then, Start.ToString() gave me "3/5/2013 12:0:00 AM", which is the exact same result as the previous one. I need to keep the original formatting. How may I do it? 

Thanks in in advance!. 


Total Post:604

Posted on    May-10-2013 9:07 AM

Hi Goti!

The format you parse with does not dictate how the DateTime is formatted when you convert the date back to a string. You need to pass the format into ToString() 


Start.ToString("dd-MM-yyyy HH:mm:ss");

Don't want to miss updates? Please click the below button!

Follow MindStick