C# Datediff | C# Calculating Date Difference - c# - c# tutorial - c# net
What are the difference between dates in C# ?
- In c#, the DateTime.Substract method is used, in order to find the date/time difference between two instances of the DateTime method.
- The date method does not change the value of the DateTime instance on which the method has been invoked.
- The result of the process is stored in the new TimeSpan structure.
- DateTime.Substract is specifies to one DateTime can be subtracted from another.
- This method returns the difference between the two dates and time.

Date difference in csharp

C# diffdates

- Go to tool box and select the Button option.
- After dragging and dropping a Button component from Toolbox to a Form.
C# Sample Code - C# Examples:
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 wikitechy_CalculatingDateDifference
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
System.DateTime Date1 = new System.DateTime(2016, 06, 24);
System.DateTime Date2 = new System.DateTime(2016, 07, 24);
System.TimeSpan variance = Date2.Subtract(Date1);
System.TimeSpan variance1 = Date2 - Date1;
MessageBox.Show(variance.ToString());
}
}
}
Code Explanation:

- These specifies to initializes a new instance (Datae1) of the DateTime structure to the specified 2016, 06, 24 (year, month(June), and date).
- In this example specifies to initializes a new instance (Datae2) of the DateTime structure to the specified 2016, 07, 24 (year, month(July), and date).
- The TimeSpan datatype is used to represent time interval, these calculating the Date Difference is through the use of the Subtraction
- System.TimeSpan variance1 = Date2 - Date1; specifies to difference between two dates i.e. (Date2-Date1) using subtracting method.
- MessageBox.Show(data.ToString()) displays a message box with the specified string. Here is returns this string variance.
Sample C# examples - Output :

- Here in this output the Button will display the data values from the DateDiffrence and displays it by clicking it.
- In this output we display the "30.00:00:00" which specifies difference between (Date2 - Date1 = 30days) displayed in message box window.