In this article, we will learn how to convert DateTime
from a specific format to 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 and so on…
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;
}
}
}
dd-MM-yyyy to yyyy-MM-dd => 2021-01-02
dd-MM-yyyy to MM/dd/yyyy => 01/02/2021
dd-MM-yyyy to MMM dd, yyyy => Jan 02, 2021
dd-MM-yyyy to MMM dd, yyyy => Jan 02, 2021
MM/dd/yyyy to dd-MM-yyyy => 02-01-2021
MM/dd/yyyy to dd MMM, yyyy => 02 Jan, 2021
MM.dd.yyyy to MMM dd, yyyy => Jan 02, 2021
MM.dd.yyyy to dd-MM-yyyy => 02-01-2021
MM.dd.yyyy to dd MMM, yyyy => 02 Jan, 2021
MM.dd.yyyy to dd MMM, yyyy => Enter the valid Date as Per Input Format
I hope this article will help you to understand how to convert DateTime From a specific format to a specific format in C#.
Share your valuable feedback, please post your comment at the bottom of this article. Thank you!
Comments