Wednesday, May 14, 2008

How to Get Date and Time Difference in C#

Hello guyz. I have example here on how to get the difference of date and time in C#.

Step: 1
Create a timer then set the interval to 10 and enabled to true.

Step: 2
Create 3 Label. Label1,Label2,Label3.

Step: 3
Create 2 Datepicker. Name it dtfrom and dtto.

Step: 4
Create a Button1

Then Copy this Code at your form:

private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
{label1.Text = dtfrom.Value.ToString();}
private void timer1_Tick(object sender, EventArgs e)
{label2.Text = string.Format("{0:G}", DateTime.Now);}
private void button1_Click(object sender, EventArgs e)
TimeSpan ts = new TimeSpan();
DateTime dt1,dt2 = new DateTime();
dt1 = dtfrom.Value;
dt2 = Convert.ToDateTime(label2.Text);
label3.Text = ts.ToString(); //Answer

No comments: