How to Format Date Using C#

Submitted by: 
Visitors have accessed this post 185 times.

In this tutorial, I will teach you how to format the date using c#. This method has the ability to convert the date into the string that will help you format it according to your desire, whether it is long, short, time or custom format. This simple method is so powerful when you are dealing with dates. Simply follow the procedure below.

Creating Application

Step 1

Open Microsoft Visual Studio 2015 and create a new windows form application for c#.

Step 2

Do the form just like shown below.
figure 1

Step 3

Press F7 to open the code editor. In the code editor, create a method to format the date and put it into the listbox.

  2. private void strDate(string strFormat,DateTime givendate)
  3. {
  4. try
  5. {
  6. DateTime date = new DateTime();
  8. date = givendate;
  10. listBox1.Items.Add(String.Format("{0:" + strFormat + "}", date));
  12. }
  13. catch (Exception ex)
  14. {
  15. MessageBox.Show(ex.Message);
  16. }
  17. }

Step 4

Double click the button and write the following code to execute the method when the button is clicked.

  2. private void button1_Click(object sender, EventArgs e)
  3. {
  4. strDate(textBox1.Text, dateTimePicker1.Value);
  6. }

The complete source code is included. You can download it and run it on your computer.
For any questions about this article. You can contact me @
Email – [email protected]
Mobile No. – 09305235027 – TNT
Or feel free to comment below.

Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • You may insert videos with [video:URL]
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <th> <img> <h1> <h2> <h3> <iframe> [video]
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <html4strict>, <java>, <javascript>, <mysql>, <php>, <python>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.