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.
  • learn c# tutorials - date difference programs in csharp - c# programs

    Date difference in csharp

  • The date method does not change the value of the DateTime instance on which the method has been invoked.
  •  C#  diffdates

    C# diffdates

  • 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.
 calculating date
  1. Go to tool box and select the Button option.
  2. 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:

 calculating date code
  1. These specifies to initializes a new instance (Datae1) of the DateTime structure to the specified 2016, 06, 24 (year, month(June), and date).
  2. 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).
  3. The TimeSpan datatype is used to represent time interval, these calculating the Date Difference is through the use of the Subtraction
  4. System.TimeSpan variance1 = Date2 - Date1; specifies to difference between two dates i.e. (Date2-Date1) using subtracting method.
  5. MessageBox.Show(data.ToString()) displays a message box with the specified string. Here is returns this string variance.

Sample C# examples - Output :

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

Related Searches to C# Datediff | C# Calculating Date Difference