Convert date in string to DateTime with same format

Total Post:119


 1438  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!. 
  1. Re: Convert date in string to DateTime with same format

    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");


Please check, If you want to make this post sponsored

You are not a Sponsored Member. Click Here to Subscribe the Membership.