已知日期A是星期二、四、日中的一天。求A到B这段日期内共有几个星期二、四、日?

解决方案 »

  1.   

    { 函数参数说明:A--The Old Date   B--The Nearest Date  Dw--说明要统计的是周几}
    { Dw == 1  统计周日数量
      Dw == 2  统计周一数量
      Dw == 3  统计周二数量
      Dw == 4  统计周三数量
      Dw == 5  统计周四数量
      Dw == 6  统计周五数量
      Dw == 7  统计周六数量 }function MyFuc( A,B :TDateTime; Dw :integer):integer;
    var
       X,Y,Z:integer;
    begin
       X := DayOfWeek( A );
       {  if not( X in [0,2,4]) then   //如果你需要限制日期 A 只能
             begin                     //是 周二、四、日
             Result := -1 ;            //那么你可以把我注掉的
             exit;                     //这个判断语句启用
             end;    }                 //,于是,非周二四日,则返回-1。
       Z := Round( B - A ) mod 7 ;
       if Z+X <= 7 then
          if (Dw >= X) and (Dw <= Z+X) then Y := 1 else Y := 0
          else
          if Z+X-7 >= Dw then Y := 1
             else if (Dw <= Z+X) and (Dw >= X) then Y := 1 else Y := 0;   Result := Round( B - A ) div 7   + Y ;
    end;procedure TForm1.Button1Click(Sender: TObject); //调用函数举例
    var
       A,B:TDateTime;
    begin
       A := VarToDatetime('2006-11-18');
       B := VarToDatetime('2006-12-16');
       ShowMessage( inttostr(MyFuc(a,b,1)) + '个星期日' + #13
                  + inttostr(MyFuc(a,b,2)) + '个星期一' + #13
                  + inttostr(MyFuc(a,b,3)) + '个星期二' + #13
                  + inttostr(MyFuc(a,b,4)) + '个星期三' + #13
                  + inttostr(MyFuc(a,b,5)) + '个星期四' + #13
                  + inttostr(MyFuc(a,b,6)) + '个星期五' + #13
                  + inttostr(MyFuc(a,b,7)) + '个星期六');
    end;
      

  2.   

    先谢谢lihuasoft(学习低调做人)你的回复,我所说的"求A到B这段日期内共有几个星期二、四、日?"是指总数,不用分开计.下面是我用的方法,请指教。
    procedure TForm1.Button1Click(Sender: TObject);
    var i,j,k:integer;
    begin
      i:=daysbetween(vartodatetime(edit1.Text),vartodatetime(edit2.Text));
      j:=dayoftheweek(vartodatetime(edit1.Text));
      k:=i div 7;
      i:=i mod 7;
      if j=2 then i:=(i+1)div 3
      else if j=4 then i:=(i-1)div 2
      else if j=7 then begin
        if i>4 then i:=4;
        i:=i div 2;
      end;
      j:=k*3+i;
      edit3.Text:= inttostr(j);
    end;
      

  3.   

    偶能斗胆回一下楼主的贴子,已经感激不尽了,因此楼主不必感谢。而“指教”,偶更不敢当。测试了一下楼主的代码:2006-12-12(周二)~2006-12-31  结果是:8而如果用我的函数: 
    var
       A,B:TDateTime;
    begin
       A := VarToDatetime(Edit1.Text); //Edit1.text:='2006-12-12'
       B := VarToDatetime(Edit2.Text); //Edit2.text:='2006-12-31'
       ShowMessage(IntToStr( MyFuc(a,b,1)+MyFuc(a,b,3)+MyFuc(a,b,5)));
    end;
    测试结果是: 9看到楼主的题目时,我就想:我应该尽可能地写一个通用的函数,这个函数要不仅仅只能统计“周二、周四、周日”,而是应该周几都能统计。于是我就那样写了。
    至于您说的“总和”,无非就用算术上最普通的加法即可解决:MyFuc(a,b,1)+MyFuc(a,b,3)+MyFuc(a,b,5)我说的不当之处,望楼主原谅。
      

  4.   

    噢,对了,我的代码是不计A这天的,因这天已知是星期二、四、日中的了。即B-A后的时间段。