Loading

DateTime Picker Control Using C#

Submitted by: 
Language: 
Visitors have accessed this post 1578 times.

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.

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.

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.

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.

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.

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. }

Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <th>
  • You may insert videos with [video:URL]
  • 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.