Convert dd/MM/yyyy to MM/dd/YYYY

Lajja Thaker picture Lajja Thaker · Sep 8, 2012 · Viewed 33.6k times · Source

I need to convert "28/08/2012" to MM/dd/YYYY format that means "08/28/2012".
How can I do that?

I am using below code , but it threw exception to me.

DateTime.ParseExact("28/08/2012", "ddMMyyyy",  CultureInfo.InvariantCulture)

Answer

Nikhil Agrawal picture Nikhil Agrawal · Sep 8, 2012

but it threw exception to me

Problem:

Your date contains / seperator ("28/08/2012") and you are not giving that in your date string format ("ddMMyyyy").

Solution:

It should be "dd/MM/yyyy".

This way

DateTime.ParseExact("28/08/2012", "dd/MM/yyyy", CultureInfo.InvariantCulture)
                        .ToString("MM/dd/yyyy", CultureInfo.InvariantCulture);

After doing that we will receive a DateTime object with your populated dates which is transferred to string using .ToString() with desired date format "MM/dd/yyyy" and optional culture info CultureInfo.InvariantCulture.