Come convertire DateTime da un formato specifico in un formato specifico in C#

Come convertire DateTime da un formato specifico in un formato specifico in C#

In questo articolo impareremo come convertire DateTime da un formato specifico a un formato specifico in C#. Ho trovato questo modo per gestire la conversione della data da un formato specifico a un formato specifico. Spero che quando ti sei bloccato con la conversione DateTime da un formato specifico a un altro formato specifico, questo esempio riportato di seguito ti aiuterà.

Ad esempio, ecco alcuni vari formati di date di cui abbiamo bisogno per convertire la data in un formato specifico come indicato di seguito;

Converti data da gg-MM-aaaa a aaaa-MM-gg

Converti data da gg-MM-aaaa a MM/gg/aaaa

Converti data da gg-MM-aaaa a MMM gg, aaaa

Converti data da MM/gg/aaaa a aaaa-MM-gg

Converti data da MM.gg.aaaa a aaaa-MM-gg

Converti data da MMM gg, aaaa a aaaa-MM-gg e così via...

Converti DateTime da un formato specifico a un formato specifico

Esempio - Converti DateTime da un formato specifico a un formato specifico
using System;

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

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

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

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

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

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

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

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

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

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

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

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

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


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

        public static string DateTimeConversion(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;
        }
    }

}
Uscita

gg-MM-aaaa a aaaa-MM-gg => 2021-01-02

gg-MM-aaaa a MM/gg/aaaa => 01/02/2021

gg-MM-aaaa a MMM gg, aaaa => 02 gennaio 2021

gg-MM-aaaa a MMM gg, aaaa => 02 gennaio 2021

MM/gg/aaaa a gg-MM-aaaa => 02-01-2021

MM/gg/aaaa a gg MMM, aaaa => 02 gennaio 2021

MM.gg.aaaa a MMM gg, aaaa => 02 gennaio 2021

MM.gg.aaaa a gg-MM-aaaa => 02-01-2021

MM.gg.aaaa a gg MMM, aaaa => 02 gennaio 2021

Da MM.gg.aaaa a gg MMM, aaaa => Inserisci la data valida come da formato di input

Spero che questo articolo ti aiuti a capire come convertire DateTime da un formato specifico in un formato specifico in C#.

Condividi il tuo prezioso feedback, per favore pubblica il tuo commento in fondo a questo articolo. Grazie!