Calculate difference between two dates by DateTimePicker in C#

The following code find out difference in days between two date time pickers selected date.

Step 1 :

Place two date time pickers in your C# windows form.

Step 2:

Place a button which calculates difference on pressing(event firing)

Step 3:

Place a label to display difference between two dates.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            // convert difference to string 
            label1.Text = Convert.ToString((dateTimePicker2.Value - dateTimePicker1.Value).TotalDays);
        }
    }
}

Output

One thought on “Calculate difference between two dates by DateTimePicker in C#

  1. Thanx, but on the lable getting the decimal value (18-05-2014)-(15-05-2014)=2.9999999999981;

Comments are closed.