Convert DateTime to specific format in c#

using System;

namespace usingcsharp
{
    class Program
    {
        /* How to Convert DateTime from a Specific Format in C# */
        static void Main(string[] args)
        {
            //Format of Date1 => dd-MM-yyyy
            string Date1 = "31-01-2021";

            //Format of Date2 => MM/dd/yyyy
            string Date2 = "01/31/2021";

            //Format of Date3 => MM.dd.yyyy
            string Date3 = "01.31.2021";

            // dd-MM-yyyy to yyyy-MM-dd
            Console.WriteLine("dd-MM-yyyy to yyyy-MM-dd => {0}",DateTimeConversionFormat(Date1,"dd-MM-yyyy","yyyy-MM-dd"));

            // dd-MM-yyyy to MM/dd/yyyy
            Console.WriteLine("dd-MM-yyyy to MM/dd/yyyy => {0}", DateTimeConversionFormat(Date1, "dd-MM-yyyy", "MM/dd/yyyy"));

            // dd-MM-yyyy to MMM dd, yyyy
            Console.WriteLine("dd-MM-yyyy to MMM dd, yyyy => {0}", DateTimeConversionFormat(Date1, "dd-MM-yyyy", "MMM dd, yyyy"));

            // MM/dd/yyyy to MMM dd, yyyy
            Console.WriteLine("dd-MM-yyyy to MMM dd, yyyy => {0}", DateTimeConversionFormat(Date2, "MM/dd/yyyy", "MMM dd, yyyy"));

            // MM/dd/yyyy to dd-MM-yyyy
            Console.WriteLine("MM/dd/yyyy to dd-MM-yyyy => {0}", DateTimeConversionFormat(Date2, "MM/dd/yyyy", "dd-MM-yyyy"));

            // MM/dd/yyyy to dd MMM, yyyy
            Console.WriteLine("MM/dd/yyyy to dd MMM, yyyy => {0}", DateTimeConversionFormat(Date2, "MM/dd/yyyy", "dd MMM, yyyy"));

            // MM.dd.yyyy to MMM dd, yyyy
            Console.WriteLine("MM.dd.yyyy to MMM dd, yyyy => {0}", DateTimeConversionFormat(Date3, "MM.dd.yyyy", "MMM dd, yyyy"));

            // MM.dd.yyyy to dd-MM-yyyy
            Console.WriteLine("MM.dd.yyyy to dd-MM-yyyy => {0}", DateTimeConversionFormat(Date3, "MM.dd.yyyy", "dd-MM-yyyy"));

            // MM.dd.yyyy to dd MMM, yyyy
            Console.WriteLine("MM.dd.yyyy to dd MMM, yyyy => {0}", DateTimeConversionFormat(Date3, "MM.dd.yyyy", "dd MMM, yyyy"));

            // MM.dd.yyyy to dd MMM, yyyy
            Console.WriteLine("MM.dd.yyyy to dd MMM, yyyy => {0}", DateTimeConversionFormat("01-31-2021", "MM.dd.yyyy", "dd MMM, yyyy"));


            //Hit ENTER to exit the program
            Console.ReadKey();
        }

        public static string DateTimeConversionFormat(string Date, string DateInputFormat, string DateOutputFormat)
        {
            string ConvertedDate = "";
            if (string.IsNullOrEmpty(Date))
            {
                ConvertedDate = "Please Provide the Valid DateTime";
            }
            else
            {
                DateTime Outputdate;

                if (DateTime.TryParseExact(Date, DateInputFormat, System.Globalization.CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.None, out Outputdate))
                {
                    ConvertedDate = Outputdate.ToString(DateOutputFormat);
                }
                else
                {
                    ConvertedDate = "Enter the valid Date as Per Input Format";
                }
            }
            return ConvertedDate;
        }
    }

}

Convert DateTime to specific format in c#

 

In this article, you will learn how to convert DateTime to specific format in c#

 

DateTime.ParseExact("31/01/21 10151:10", "yy/MM/dd HH:mm:ss", System.Globalization.CultureInfo.InvariantCulture).ToString("MMM. dd, yyyy HH:mm:ss");

 

 

2021-10-03