C# Timespan Difference Between Two Datetime
In This C# Tutorial We Will See How To Get The Difference Between Two DateTimePicker In
- Days.
- Hours.
- Minutes.
- Seconds
- Millisconds.
Using CSharp Programming Language
- Days.
- Hours.
- Minutes.
- Seconds
- Millisconds.
Using CSharp Programming Language
Source Code :
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class difference_between_two_dates : Form
{
public difference_between_two_dates()
{
InitializeComponent();
}
private void BTN_CALC_Click(object sender, EventArgs e)
{
DateTime date1 = dateTimePicker1.Value;
DateTime date2 = dateTimePicker2.Value;
TimeSpan difference = date2 - date1;
lbl_Days.Text = "Days: " + difference.TotalDays.ToString();
lblH.Text = "Hours: " + difference.TotalHours.ToString();
lblM.Text = "Minutes: " + difference.TotalMinutes.ToString();
lblS.Text = "Seconds: " + difference.TotalSeconds.ToString();
lblMS.Text = "Milliseconds: " + difference.TotalMilliseconds.ToString();
}
}
}
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class difference_between_two_dates : Form
{
public difference_between_two_dates()
{
InitializeComponent();
}
private void BTN_CALC_Click(object sender, EventArgs e)
{
DateTime date1 = dateTimePicker1.Value;
DateTime date2 = dateTimePicker2.Value;
TimeSpan difference = date2 - date1;
lbl_Days.Text = "Days: " + difference.TotalDays.ToString();
lblH.Text = "Hours: " + difference.TotalHours.ToString();
lblM.Text = "Minutes: " + difference.TotalMinutes.ToString();
lblS.Text = "Seconds: " + difference.TotalSeconds.ToString();
lblMS.Text = "Milliseconds: " + difference.TotalMilliseconds.ToString();
}
}
}