Minifycode 2021-10-03 Viewed 1.4K times C#

In this article, we will learn how to convert DateTime from a specific format in C#. I found this way for handling the date conversion from a specific format to a specific format. I hope when you stuck with DateTime conversion from one specific format to another specific format, this example given below will help you.

For example, here are some various formats of dates from we need to convert date to a specific format as given below;

Convert Date from dd-MM-yyyy to yyyy-MM-dd

Convert Date from dd-MM-yyyy to MM/dd/yyyy

Convert Date from dd-MM-yyyy to MMM dd, yyyy

Convert Date from MM/dd/yyyy to yyyy-MM-dd

Convert Date from MM.dd.yyyy to yyyy-MM-dd

Convert Date from MMM dd, yyyy to yyyy-MM-dd

 

using System;

namespace minifycode
{
    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");

 

 

Convert DateTime to specific format in c# with example
C# is a programming language developed by Microsoft that runs on the .NET Framework. C# is used to develop web, desktop, mobile, games and much more application. C# is a object-oriented programming language developed by Microsoft within its .NET Framework. Led by Anders Hejlsberg, your basic C# programming and will also take you through various advanced concepts related to C# programming language. C# such as control statements, objects and classes, inheritance, constructor, destructor, this, static, sealed, polymorphism, abstraction, abstract class, interface, File IO, Collections, namespace, encapsulation, properties, indexer, arrays, strings, regex, exception handling, multithreading etc. For example... using System; namespace MinifyCode { class Program { static void Main(string[] args) { Console.WriteLine("Hello Minify Code"); } } } Output: Hello Minify Code In this article you will learn, what is server side controls. We will discuss each of these objects in due time. In this tutorial we will explore the Server object, the Request object, and the Response object. Session Application Cache Request Response Server User Trace Server Object The Server object in Asp.NET is an instance of the System.Web.HttpServerUtility class. The HttpServerUtility class provides numerous properties and methods to perform many type of jobs. Methods and Properties of the Server object The methods and properties of the HttpServerUtility class are exposed through the intrinsic Server object provided by ASP.NET. using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Optimization; using System.Web.Routing; using System.Web.Security; using System.Web.SessionState; using System.Data.Entity; namespace minifycode { public class Global : HttpApplication { void Application_Start(object sender, EventArgs e) { // Code that runs on application startup RouteConfig.RegisterRoutes(RouteTable.Routes); BundleConfig.RegisterBundles(BundleTable.Bundles); // Initialize the product database. Database.SetInitializer(new ProductDatabaseInitializer()); // Create custom role and user. RoleActions roleActions = new RoleActions(); roleActions.AddUserAndRole(); // Add Routes. RegisterCustomRoutes(RouteTable.Routes); } void RegisterCustomRoutes(RouteCollection routes) { routes.MapPageRoute( "ProductsCategoryRoute", "Category/{categoryName}", "~/ProductList.aspx" ); routes.MapPageRoute( "ProductNameRoute", "Product/{productName}", "~/ProductDetails.aspx" ); } } }