Skip to main content

DateTime Picker Control Using C#


In this tutorial I’m going to show you how to use a DateTimePicker control in C#. The DateTimepicker control allows the user to select a date and a time and to display the date and time with specified format. To start creating this application, let’s create a new project in C# and we will call it as “Dates”. Then from the toolbox drag a DateTmePicker and four Buttons. The design will look like as shown below.
da1

Next, to add functionality to our application double click the “Long” button. And add the following code.

  1. private void button1_Click(object sender, EventArgs e)
  2. {
  3. //set datetimepicker format to long
  4. dateTimePicker1.Format = DateTimePickerFormat.Long;
  5. //display the value of dateTimePicker in a Message Dialog Box
  6. MessageBox.Show(dateTimePicker1.Text);
  7.  
  8. }

The code above will be executed when the “Long” button is clicked. Then it will display the date in a Long format using a dialog box. And this look like as shown below.

da2
Next, For the “Short” button here’s the following code.

  1. private void button2_Click(object sender, EventArgs e)
  2. {
  3. //set datetimepicker format to short
  4. dateTimePicker1.Format = DateTimePickerFormat.Short;
  5. //display the value of dateTimePicker in a Message Dialog Box
  6. MessageBox.Show(dateTimePicker1.Text);
  7. }

if the “Short” buttong is click it will display a short format of date in a dialog box. And it looks like as shown below.

da3
And for the “Time” button, here’s the following code.

  1. private void button3_Click(object sender, EventArgs e)
  2. {
  3. //set datetimepicker format to time
  4. dateTimePicker1.Format = DateTimePickerFormat.Time;
  5. //display the value of dateTimePicker in a Message Dialog Box
  6. MessageBox.Show(dateTimePicker1.Text);
  7. }

The output for “Time” button is look like as shown below.
da4
Then, for the “Custom” button, here’s the following code.
  1. private void button4_Click(object sender, EventArgs e)
  2. {
  3. // Set the CustomFormat string.
  4. dateTimePicker1.CustomFormat = "MMMM dd, yyyy - dddd";
  5. dateTimePicker1.Format = DateTimePickerFormat.Custom;
  6. MessageBox.Show(dateTimePicker1.Text);
  7. }

And finally the output will look like as shown below.
da5

And here’s all the code used in this application.

  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9.  
  10. namespace Dates
  11. {
  12. public partial class Form1 : Form
  13. {
  14. public Form1()
  15. {
  16. InitializeComponent();
  17. }
  18.  
  19. private void button1_Click(object sender, EventArgs e)
  20. {
  21. //set datetimepicker format to long
  22. dateTimePicker1.Format = DateTimePickerFormat.Long;
  23. //display the value of dateTimePicker in a Message Dialog Box
  24. MessageBox.Show(dateTimePicker1.Text);
  25.  
  26. }
  27.  
  28. private void button2_Click(object sender, EventArgs e)
  29. {
  30. //set datetimepicker format to short
  31. dateTimePicker1.Format = DateTimePickerFormat.Short;
  32. //display the value of dateTimePicker in a Message Dialog Box
  33. MessageBox.Show(dateTimePicker1.Text);
  34. }
  35.  
  36. private void button3_Click(object sender, EventArgs e)
  37. {
  38. //set datetimepicker format to time
  39. dateTimePicker1.Format = DateTimePickerFormat.Time;
  40. //display the value of dateTimePicker in a Message Dialog Box
  41. MessageBox.Show(dateTimePicker1.Text);
  42. }
  43.  
  44. private void button4_Click(object sender, EventArgs e)
  45. {
  46. // Set the CustomFormat string.
  47. dateTimePicker1.CustomFormat = "MMMM dd, yyyy - dddd";
  48. dateTimePicker1.Format = DateTimePickerFormat.Custom;
  49. MessageBox.Show(dateTimePicker1.Text);
  50. }
  51. }
  52. }

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.

Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.

FOR YOUR OWN SAFETY, PLEASE:

1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.

Add new comment

CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.