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

Total Post:119

Points:835
Posted on    May-10-2013 2:00 AM

 C# C# 
Ratings:


 1 Reply(s)
 809  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!. 


AVADHESH PATEL

Total Post:604

Points:4228
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() 

i.e.

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

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

Follow MindStick