Changing Date Time Format in C# according to user requirement

our database was taking date in string format so the problem was the date was showing in c# 8/13/2010 2:04:11 PM in this format where as my database wants it in 08132010 format so as usual i tried String.Format method to get the solution

our problem was so small but some times small problems will create big Time waste so here is the solution for getting date in string format

DateTime FrDate = new DateTime(2010,12,31);

//adding this value to session

String.Format(“{0:ddMMyyyy}”, Session[“FrDate”])


String s = String.Format(“{0:mmDDyyyy}”,FrDate)


Update to this article

if you need to convert the string from 31/01/2014 11:10:12 AM
to 01-31-2014 11:10:12 AM then you can do using following code

DateTime dt = Convert.ToDateTime(“31/01/2014 11:10:12 AM”);
string newdt = dt.ToString(“dd-MM-yyyy HH:mm:ss tt”);



About kakani santosh kumar

Working as Software Engineer in Prithvi Information Solutions having 3 years of Experience in Dotnet Framework ranging from 1.1 to 3.5
This entry was posted in Programming and tagged , , , . Bookmark the permalink.

One Response to Changing Date Time Format in C# according to user requirement

  1. santosh says:

    this is also used

    string MyNewDateString;

    DateTime MyDateConverted = DateTime.ParseExact(“05192008”, “MMddyyyy”, System.Globalization.CultureInfo.InvariantCulture);

    MyNewDateString = MyDateConverted.ToString(@”dd-MM-yyyy”);

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s