计算日期和时间差 [英] Calculate date and time difference

查看:105
本文介绍了计算日期和时间差的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我想在两分钟之内得到差额投注.
我有4个文本框,如下所示
1. txtDateIn
2. txtTimeIn
3. txtDateOut
4. txtTimeOut

我正在获取日期之间的差异,也获取了时差,但是我的问题是结果不正确...

如果我输入相同的日期,即
txtDateOut = 3/3/3010,txtDateIn = 3/3/3010,
txtTimeOut = 12:06 AM,txtTimeOut = 12:07 AM,
那么结果必须相差1分钟
但结果为24.这是错误的.

代码在下面给出
请帮我....

Hi,

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();

推荐答案

您应该考虑遍历调试器以逐步研究结果.您的整体方法是错误的. d2和d1具有时间分量,而t2和t1具有日期分量.我认为您不会以这种方式获得预期的结果.我认为您应该将d1的日期与t1的时间合并,将d2和t2的日期合并,然后将其相减.

在JSOP中:我5'd-真是个好主意……:)
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... :)


sagar55写道:
sagar55 wrote:

TimeSpan total_diff = TimeSpan .Parse(date_diff).Add(TimeSpan .Parse(time_diff));

TimeSpan total_diff=TimeSpan .Parse(date_diff ).Add( TimeSpan .Parse (time_diff ));


问题出在这行代码中.



Problem is in this line of code.


sagar55写道:
sagar55 wrote:

TimeSpan .Parse(time_diff)

TimeSpan .Parse (time_diff)


当您指定时,Timespan被创建为Day的默认值.因此,您定义了要添加的1天,因此实际上得到了24小时,甚至不到24分钟!


现在,我们知道了这一点可以解决它,但是再次出现问题,因为您分别定义日期和时间并分别处理.
您应该执行以下操作:


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();


您可以尝试在调试器中逐步进行调试.我敢打赌,您马上就会发现出了什么问题.

除此之外,为什么还要花这么多代码呢?

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;










这篇关于计算日期和时间差的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆