CONVERT DATE IN STRING TO DATETIME WITH SAME FORMAT

Goti Bandu

Total Post:119

Points:835
Posted by  Goti Bandu
C# 
C#
 1055  View(s)
Ratings:
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. AVADHESH PATEL

    Post:604

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

    i.e.

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

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!