delphi2007 教程

delphi2007 教程

首页 新随笔 联系 聚合 管理
  1013 Posts :: 0 Stories :: 28 Comments :: 0 Trackbacks
如何计算某时间段内有几个星期几? Delphi / Windows SDK/API
http://www.delphi2007.net/DelphiBase/html/delphi_20061216151111140.html
已知日期A是星期二、四、日中的一天。求A到B这段日期内共有几个星期二、四、日?

{   函数参数说明: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;

注意,注掉的那个判断部分,有个小错,即:你把那个集合[0,2,4]改为[1,3,5]即可!!  
   
  测试结果:(2006-11-18~2006-12-16)  
   
  4个星期日  
  4个星期一  
  4个星期二  
  4个星期三  
  4个星期四  
  4个星期五  
  5个星期六  
 

先谢谢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;

偶能斗胆回一下楼主的贴子,已经感激不尽了,因此楼主不必感谢。而“指教”,偶更不敢当。  
   
  测试了一下楼主的代码: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)  
   
  我说的不当之处,望楼主原谅。

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

posted on 2008-10-14 16:34 delphi2007 阅读(194) 评论(0)  编辑 收藏 引用
只有注册用户登录后才能发表评论。