如何减去日期? [英] How to subtract dates?

查看:81
本文介绍了如何减去日期?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨我有下面的代码,但是当我运行程序时,计数总是会停留在零......

例如。如果我输入2014年3月3日和daysInWeek为4,它应该返回2计数,但它给我0代替。任何帮助将不胜感激。



  string  datetime = txtEffectiveDate.Text ; 
DateTime startDate = Convert.ToDateTime(datetime); // 用户输入的日期
int daysInWeek = Convert.ToInt32(txtDaysInWeek.Text); // no。用户输入的天数
DateTime previousDate = startDate.AddDays(-daysInWeek); // 从上面的ori日期中扣除后的日期

TimeSpan preDiff = previousDate - 开始日期;
int preDays = preDiff.Days;

for var j = 0 ; j < = preDays; j ++)
{
var pretestDate = startDate.AddDays(-j);
switch (pretestDate.DayOfWeek)
{
case DayOfWeek。星期六:
count ++;
break ;
case DayOfWeek.Sunday:
count ++;
break ;

}
}

MessageBox.Show( 您有 + count + 休假日);

解决方案

嗯...除非是周六或周日,否则你不会做任何计数,所以你可能需要看一下,但是主要问题非常明显,如果你用调试器快速运行。

在这一行放一个断点:

  code-keyword> var  j =  0 ; j <  = preDays; j ++)

并查看preDays的值。

它是什么?

答案,-4。

并且是0< = -4?

编号所以你的循环永远不会被执行 - 就像你曾经尝试过的那样使用调试器逐步执行代码。也许你想扭转这种局面:

 TimeSpan preDiff = previousDate  -  startDate; 

对此:

< pre lang =c#> TimeSpan preDiff = startDate - previousDate;

但是......因为这只是你想到的数字 - daysInWeek - 为什么要这样做?


使用调试器逐步执行该代码会向您显示您的 TimeSpan 值为负数。将其更改为:

 TimeSpan preDiff = startDate  -  previousDate; 


Hi i have the codes below, but when i run the program, the count would always stuck at zero...
eg. if i entered 3/3/2014 and daysInWeek as 4, it should return 2 for the count but it give me 0 instead. Any help would be appreciated.

string datetime = txtEffectiveDate.Text;
DateTime startDate = Convert.ToDateTime(datetime); // date entered by users
int daysInWeek = Convert.ToInt32(txtDaysInWeek.Text); //no. of days entered by users
DateTime previousDate = startDate.AddDays(-daysInWeek); // date after subtracted from ori dates above

TimeSpan preDiff = previousDate - startDate;
int preDays = preDiff.Days;
 
for (var j = 0; j <= preDays; j++)
{
   var pretestDate = startDate.AddDays(-j);
   switch (pretestDate.DayOfWeek)
   {
      case DayOfWeek.Saturday:
      count++;
      break;
      case DayOfWeek.Sunday:
      count++;
      break;
 
   }
}
 
MessageBox.Show("You have " + count + " days off");

解决方案

Well...you don't do anything with count, unless it's a Saturday or Sunday, so you probably need to look at that, but the main problem is pretty obvious, if you quickly run trough with the debugger.
Put a breakpoint on this line:

for (var j = 0; j <= preDays; j++)

And look at the value of preDays.
What is it?
Answer, -4.
And is 0 <= -4 ?
No. So your loop is never executed - as you would have seen if you had tried using the debugger to step through the code. Probably, you wanted to reverse this:

TimeSpan preDiff = previousDate - startDate;

To this:

TimeSpan preDiff = startDate - previousDate;

But...since this just works out the number you first thought of - daysInWeek - why do it at all?


Stepping through that code with your debugger would have shown you that your TimeSpan value was negative. Change it to:

TimeSpan preDiff = startDate - previousDate;


这篇关于如何减去日期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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