Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert dd/MM/yyyy to MM/dd/YYYY [duplicate]

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)
like image 889
Lajja Thaker Avatar asked Sep 08 '12 05:09

Lajja Thaker


People also ask

How do you convert mm/dd/yyyy to mm yyyy in Excel?

There is a formula that can quickly convert dd/mm/yyyy to mm/dd/yyyy date format. Select a blank cell next to the dates you want to convert, type this formula =DATE(VALUE(RIGHT(A9,4)), VALUE(MID(A9,4,2)), VALUE(LEFT(A9,2))), and drag fill handle over the cells which need to use this formula.

How do I insert date in dd mm yyyy format in Excel?

Select the cells you want to format. Press CTRL+1. In the Format Cells box, click the Number tab. In the Category list, click Date, and then choose a date format you want in Type.

How do I change date format to dd mm yyyy in Windows 11?

Select the date and time tab. Change date and time>change calendar settings. In the format look for English(United States) and select. In abbreviated date and time Choose the option MM/dd/yyyy.


1 Answers

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.

like image 103
Nikhil Agrawal Avatar answered Sep 30 '22 09:09

Nikhil Agrawal