How to Format Date Using C#

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#. ps1

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

This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.
6 + 3 =
Solve this simple math problem and enter the result. E.g. for 1+3, enter 4.