求任意两个时间之间经过每一天的5点至10点这个时间段的总秒数的公式或函数?
期待您的完全解答,谢谢!(区分任意两个时间的所有情况写一个函数或公式)
例一:
时间一2003-11-28 02:30:23到时间二2003-11-28 05:30:23经过每一天的5点至10点这个时间段的总秒数为1823秒
例二:
时间一2003-11-28 05:30:00到时间二2003-11-28 06:30:00经过每一天的5点至10点这个时间段的总秒数为3600秒
例三:
时间一2003-11-26 05:00:00到时间二2003-11-28 06:30:00经过每一天的5点至10点这个时间段的总秒数为23400秒
...还有更多情况

解决方案 »

  1.   

    不太懂﹐DateUtils單元有個SecondsBetween函數,你試試
      

  2.   

    1、用daysbwtween(datetime1,datetime2) 算出两个时间之间的整数天;乘以3600;作为第一部分;
    2、剩下的部分就是判断不足一天的那部分;不是很难吧;应该;总秒数 := 1 +2;
      

  3.   

    function SecondsBetween(const ANow, AThen: TDateTime): Int64;Returns the number of seconds between two specified TDateTime values.
    应该可以吧:)
    至于时间由他们去选好了
      

  4.   

    测试通过
    procedure TForm1.Button1Click(Sender: TObject);
    begin
    caluate(strtodatetime('2003-12-02 01:00:00'),strtodatetime('2003-12-02 07:00:00'));
    end;function caluate(time1:tdatetime;time2:tdatetime):integer;
    var
    //time1,time2:tdatetime;
    ctime1,ctime2:tdatetime;
    str:string;
    endResult: string;
    begin
      str := datetostr(date)+' 05:00:00';
      ctime1 := strtodatetime(str);
      str := datetostr(date)+' 10:00:00';
      ctime2 := strtodatetime(str);
      if time1<time2 then
      begin
        if (time1<ctime1)and(time2<ctime1) then
          endResult:= '0';
        if (time1<ctime1)and(time2>ctime1)and(time2<ctime2) then
          endResult:=datetimetostr(time2-ctime1);
        if (time1>ctime1)and(time2<ctime2) then
          endResult:=datetimetostr(time2-time1);
        if (time1>ctime1)and(time2>ctime2) then
          endResult:=datetimetostr(ctime2-time1);
      end;
      result:=strtoint(copy(endresult,12,1))*3600
            +strtoint(copy(endresult,14,2))*60
            +strtoint(copy(endresult,17,2));
      application.MessageBox(pchar(inttostr(result)),'',mb_ok);
    end;
      

  5.   

    你的问题有问题:
    例三中的结果应该为11小时30分才对呀;
    其中26号就有5个小时经过,27号也有5个小时经过,28号有1个半小时经过;下面给出代码,调试通过!
    procedure TForm1.Button1Click(Sender: TObject);
    var
      m_dt1,m_dt2:TDateTime;
      dt1,dt2:TDateTime;
      num:Cardinal;
      function SubSecond(dt1,dt2:TDateTime):Cardinal;
      var
        ts1,ts2:TTimeStamp;
        cs1,cs2:integer;
      begin
        ts1:=DateTimeToTimeStamp(dt1);
        ts2:=DateTimeToTimeStamp(dt2);
        Result :=(ts2.Date-ts1.Date)*24*3600+((ts2.Time-ts1.Time)div 1000);
      end;begin
      m_dt1 := StrToTime('05:00:00');
      m_dt2 := StrToTime('10:00:00');
      dt1:=StrToDateTime(Edit1.Text);
      dt2:=StrToDateTime(Edit2.Text);
      num := SubSecond(dt1,dt2);  // 计数两时间的秒间
      num := num-Trunc(dt2-dt1)*19*3600;  // 减去中间间隔每日17小时
      if TimeOf(dt1)<TimeOf(m_dt1) then   // 如果起始时间<5点,则多要减掉
      begin
        m_dt1 := DateOf(dt1)+TimeOf(m_dt1);
        num := num - SubSecond(dt1,m_dt1);
      end;
      if TimeOf(dt2)>TimeOf(m_dt2) then   // 如果终止时间>10点,则多要减掉
      begin
        m_dt2 := DateOf(dt2)+TimeOf(m_dt2);
        num := num - SubSecond(m_dt2,dt2);
      end;
      Edit3.Text := IntToStr(num);end;
      

  6.   

    // 呵呵,我上面多写了一个函数,function SubSecond(dt1,dt2:TDateTime):Cardinal;
    用SecondsBetween代替就行了,改后代码如下procedure TForm1.Button1Click(Sender: TObject);
    var
      m_dt1,m_dt2:TDateTime;
      dt1,dt2:TDateTime;
      num:Cardinal;
    begin
      m_dt1 := StrToTime('05:00:00');
      m_dt2 := StrToTime('10:00:00');
      dt1:=StrToDateTime(Edit1.Text);
      dt2:=StrToDateTime(Edit2.Text);
      num := SecondsBetween(dt1,dt2);
      num := num-Trunc(dt2-dt1)*19*3600;
      if TimeOf(dt1)<TimeOf(m_dt1) then
      begin
        m_dt1 := DateOf(dt1)+TimeOf(m_dt1);
        num := num - SecondsBetween(dt1,m_dt1);
      end;
      if TimeOf(dt2)>TimeOf(m_dt2) then
      begin
        m_dt2 := DateOf(dt2)+TimeOf(m_dt2);
        num := num - SecondsBetween(m_dt2,dt2);
      end;
      Edit3.Text := IntToStr(num);end;