Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert a date string "dd/MM/yyyy" format into "MM/dd/yyyy" in asp.net c#?

Tags:

asp.net

I want to convert a string date formate "dd/MM/yyyy" into "MM/dd/yyyy" in c# example

 string d ="25/02/2012";  i want to convert into 02/25/2012
like image 589
sejal patel Avatar asked Feb 20 '13 10:02

sejal patel


People also ask

How can get date in dd mm yyyy format in asp net?

The HTML Markup consists of an ASP.Net GridView with three BoundField columns. The third BoundField column is bound to a DateTime field and the DataFormatString property is set to {0:dd/MM/yyyy} in order to display the DateTime field value in dd/MM/yyyy format.

How do I change the date format from dd mm yyyy to dd mm yyyy?

Press Ctrl+1 to open the Format Cells dialog. On the Number tab, select Custom from the Category list and type the date format you want in the Type box. Click OK to save the changes.


1 Answers

You can parse it to DateTime object using DateTime.ParseExact and later use ToString("MM/dd/yyyy")to display theDateTime` object like.

string d ="25/02/2012";
DateTime dt = DateTime.ParseExact(d, "d/M/yyyy", CultureInfo.InvariantCulture);
// for both "1/1/2000" or "25/1/2000" formats
string newString = dt.ToString("MM/dd/yyyy");

Make sure to include using System.Globalization; at the top.

like image 155
Habib Avatar answered Sep 19 '22 10:09

Habib