Well, the code that Abhinav sent is not well-written, and obviously not tested by him. He wrote:
TimeSpan dt = Convert.ToDateTime(text1.text) - Convert.ToDateTime(text2.text);
textbox3.text = "Days " + dtDiff.Days.ToString() +
"Hours " + dtDiff.TotalHours() +
"Minutes " + dtDiff.TotalMinutes();
First, I would assume that text2 should be after text1, so you would actually want to subtract text2 from text1, but I'm sure you figured that out.
Second, he declared a
TimeSpan
called "dt" and then referenced "dtDiff".
Third, to display hours, he used
TotalHours()
.
TotalHours()
will give you the difference between the two dates in only hours.
What you really want, and what I just tested would be
Dim dtDiff As TimeSpan = Convert.ToDateTime(txtDate2.Text) - _
Convert.ToDateTime(txtDate1.Text)
txtDateDiff.Text = dtDiff.Days & " Days, " & dtDiff.Hours & _
" Hours, " & dtDiff.Minutes & " Minutes, " & _
dtDiff.Seconds & " Seconds"
(It's in VB because you didn't specify which language you were working in, and I had a form already set up in VB where I could test it.