How to Compute Date Difference in C#

Today in C#, i will teach you how to create a program that finds and calculates the difference between two dates using C#. So, now let's start this Date Difference tutorial in C#! 1. Let's start with creating a Windows Form Application in C# for this tutorial by following the following steps in Microsoft Visual Studio: Go to File, click New Project, and choose Windows Application and name your project as Date Difference. 2. Next, add two DateTimePicker named DateTimePicker1 and DateTimePicker2 in the ToolBox. Then add a button named Button1 labeled as "Compute Date Difference". Take note that you have to place DateTimePicker2 above DatePicker1. You must design your interface like this: design 3. To have the current value of the two DateTimePicker component, put the following code to Form_Load.
  1. private void Form1_Load(object sender, EventArgs e)
  2. {
  3. DateTimePicker1.Value = DateTime.Today;
  4. DateTimePicker2.Value = DateTime.Today;
  5.  
  6. }
4. Now, put this code in Button1_Click. This will display the result of the DateDifference from the two DateTimePicker. Initialize firstDate variable as DateTime that holds the value of DateTimePicker1 and secondDate variable as DateTime that holds the value of DateTimePicker2.
  1. DateTime firstTime = default(DateTime);
  2. DateTime secondTime = default(DateTime);
  3.  
  4.  
  5. firstTime = DateTimePicker1.Value;
  6. secondTime = DateTimePicker2.Value;
Now, compute the date difference between the two DateTimePicker with a diff2 variable as string using the minus opearation. The variable diff2 holds the Date Difference between the secondDate and the FirstDate with their total days to minus with a ToString function to convert it to string.
  1. string diff2 = (string) ((secondTime - firstTime).TotalDays.ToString());
Then after being subtracted it is now converted into string. Then it will display the Date Difference of the two DateTimePicker.
  1. MessageBox.Show("The date difference is:" + " " + diff2.ToString());
Full source code:
  1. using System.Diagnostics;
  2. using System;
  3. using System.Windows.Forms;
  4. using System.Collections;
  5. using System.Drawing;
  6. using System.Data;
  7. using System.Collections.Generic;
  8.  
  9.  
  10.  
  11. namespace DateDifference
  12. {
  13. public partial class Form1
  14. {
  15. public Form1()
  16. {
  17. InitializeComponent();
  18.  
  19. }
  20.  
  21.  
  22. public void Button1_Click(System.Object sender, System.EventArgs e)
  23. {
  24.  
  25. DateTime firstTime = default(DateTime);
  26. DateTime secondTime = default(DateTime);
  27.  
  28.  
  29. firstTime = DateTimePicker1.Value;
  30. secondTime = DateTimePicker2.Value;
  31.  
  32.  
  33. string diff2 = (string) ((secondTime - firstTime).TotalDays.ToString());
  34.  
  35. MessageBox.Show("The date difference is:" + " " + diff2.ToString());
  36. }
  37.  
  38.  
  39.  
  40. private void Form1_Load(object sender, EventArgs e)
  41. {
  42. DateTimePicker1.Value = DateTime.Today;
  43. DateTimePicker2.Value = DateTime.Today;
  44.  
  45. }
  46. }
  47.  
  48.  
  49.  
  50. }
Press F5 to run the program. Output: output Best Regards, Engr. Lyndon Bermoy IT Instructor/System Developer/Android Developer/Freelance Programmer If you have some queries, feel free to contact the number or e-mail below. Mobile: 09488225971 Landline: 826-9296 E-mail:[email protected] Add and Follow me on Facebook: https://www.facebook.com/donzzsky Visit and like my page on Facebook at: https://www.facebook.com/BermzISware

Add new comment