Click here to Skip to main content
15,797,721 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:

I m trying to get the difference bet two days in minutes.
i m having 4 text boxes are as follows
1. txtDateIn
2. txtTimeIn
3. txtDateOut
4. txtTimeOut

i m getting the difference between the dates and also getting the time difference but my problem is that the result is not correct...

if i enter the same date i.e
txtDateOut =3/3/3010,txtDateIn =3/3/3010,
txtTimeOut=12:06 AM,txtTimeOut=12:07 AM,
then the result must be the difference of 1 minutes
but it give the result as 24. which is wrong.

code is given bellow
pls help me ....

DateTime d1,d2,t1,t2;
d2=DateTime .Parse (txtDateOut.Text );
d1=DateTime .Parse (txtDateIn.Text ) ;
t2 = DateTime.Parse(txtTimeOut.Text) ;
t1 = DateTime.Parse(txtTimeIn.Text);
TimeSpan d_diff = d2.Subtract(d1);
TimeSpan t_diff = t2.Subtract(t1);
date_diff = d_diff.TotalMinutes.ToString () ;
time_diff = t_diff.TotalMinutes.ToString (); 
TimeSpan total_diff=TimeSpan .Parse(date_diff ).Add( TimeSpan .Parse (time_diff ));
txtCountTime.Text = total_diff.TotalHours.ToString();
Updated 17-Mar-10 2:59am

You should consider walking through your debugger to investigate the result step by step. Your overall approach is wrong. d2 and d1 have a time component and t2 and t1 have a date component. I don't think that you're going to get the results you expect this way. I think you should merge the date of d1 with the time of t1, and the same for d2 and t2, and then subtract those.

From JSOP: I 5'd you - great minds, and all that... :)
Share this answer
sagar55 wrote:
TimeSpan total_diff=TimeSpan .Parse(date_diff ).Add( TimeSpan .Parse (time_diff ));

Problem is in this line of code.

sagar55 wrote:
TimeSpan .Parse (time_diff)

When you give this, Timespan is created as default of Day. So you define 1 day there to be added, thus you get 24 hours actually and not even 24 minutes!

Now, we can resolve it knowing this, but again there is a problem as you are defining date and time separately and handling separately.
You should do something like:
txtDateIn = "3/3/3010";
txtTimeIn = "12:06 AM";
txtDateOut = "3/3/3010";
txtTimeOut = "12:07 AM";

d1 = DateTime.Parse(txtDateIn + " " + txtTimeIn);
d2 = DateTime.Parse(txtDateOut + " " + txtTimeOut);
TimeSpan d_diff = d2.Subtract(d1);
txtCountTime.Text = d_diff.Minutes.ToString();
Share this answer
You could try stepping through it in the debugger. I bet you see what's wrong in no time at all.

Besides that, why are you messing around with so much code?

DateTime d1 = DateTime.Parse(txtDateIn.Text).Date + DateTime.Parse(txtTimeIn);
DateTime d2 = DateTime.Parse(txtDateOut.Text).Date + DateTime.Parse(txtTimeIn);

// here's the difference between the two date times
TimeSpan diff = d1 - d2;
int minutes = diff.TotalMinutes;

Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900