0

I want to convert dd/MM/yyyy string format to MM/dd/yyyy date Format. In my form I want to show the date to user in dd/mm/yyyy for that I have done.

But I want convert dd/MM/yyyy string date to MM/dd/yyyy date format for Search date in database.

Please Help Me!

Divyang Desai
  • 7,483
  • 13
  • 50
  • 76
parish
  • 846
  • 2
  • 18
  • 32
  • 1
    Possible duplicate of [Convert.ToDateTime('Datesrting') to required dd-MM-yyyy format of date](http://stackoverflow.com/questions/39071415/convert-todatetimedatesrting-to-required-dd-mm-yyyy-format-of-date) – Shakir Ahamed Aug 26 '16 at 06:23
  • 4
    Possible duplicate of [Converting dd/mm/yyyy formatted string to Datetime](http://stackoverflow.com/questions/15738608/converting-dd-mm-yyyy-formatted-string-to-datetime) – Jagadeesh Govindaraj Aug 26 '16 at 06:33

2 Answers2

0

First convert your dd/MM/yyyy format string to datetime using DateTime.ParseExact then convert it back as MM/dd/yyyy format date string using ToString() method

string inp = "23/08/2016"; // inp is dd/MM/yyyy format date string
DateTime date = DateTime.ParseExact(inp, "dd/MM/yyyy", CultureInfo.InvariantCulture);
string s1 = date.ToString("MM/dd/yyyy"); // s1 is now MM/dd/yyyy format date string

or simply one expression

string s2 = DateTime.ParseExact("23/08/2016", "dd/MM/yyyy", CultureInfo.InvariantCulture).ToString("MM/dd/yyyy");
Mostafiz
  • 7,243
  • 3
  • 28
  • 42
  • your answer is correct but i want convert in date time format – parish Aug 26 '16 at 07:02
  • in datetime object there is no specific format, it will present you as your system format, to get datetime in specific format you need to convert it to a specific format string, try change your pc system datetime format then you will notice datetime object will show you using that format – Mostafiz Aug 26 '16 at 07:15
  • There is an error when I try this approach: "The DateTime represented by the string is not supported in calendar System.Globalization.GregorianCalendar" – Jkyle Landicho Apr 04 '22 at 09:27
0

Try this

DateTime dateTime = new DateTime();
dateTime = Convert.ToDateTime(DateTime.ParseExact("YouDateString", "MM/dd/yyyy", CultureInfo.InvariantCulture));
Shakir Ahamed
  • 1,290
  • 3
  • 16
  • 39