How to Convert date from dd-MM-yyyy to MM-dd-yyyy in C#


The following example shows, how to convert date from dd-MM-yyyy to DateTime and MM-dd-yyyy in C#. DateTime.ParseExact used to converts the specified string representation of a date and time to its DateTime equivalent using the specified format and culture-specific format information.

using following Namespace
using System.Globalization;

Convert date from dd-MM-yyyy to DateTime

Example
string dt="18-07-2022";
DateTime DT = DateTime.ParseExact(dt, "dd-MM-yyyy", CultureInfo.InvariantCulture);

Convert date from dd-MM-yyyy to MM-dd-yyyy

Example
using System;
using System.Globalization;
public class Example
{
  public static void Main(string[] args)
  {
    string dt="18-07-2022";
    DateTime DT = DateTime.ParseExact(dt, "dd-MM-yyyy", CultureInfo.InvariantCulture);
    Console.WriteLine("Date : "+DT.ToString("MM-dd-yyyy"));
  }
}
Output:
Date : 07-18-2022