如果计算两个时间点之间的秒数。我想来想去如果说要写出个没有bug的代码实在太复杂了首先考虑分钟的度过不能直接用TDateTime得到的秒数直接相减在扩展问题如果两个分钟正在两个小时之间同样两天之间的转换两个星期两个月两个年... .怎么样是不又很复杂。软件要写的没有毛病真的要多下功夫了。vc中可以通过time函数得到1970年至今的秒数在delphi中也能得到这个秒数么用什么办法

解决方案 »

  1.   

    function WithinPastSeconds(const ANow, AThen: TDateTime;
      const ASeconds: Int64): Boolean;用函数WithinPastSeconds,delphi6在
    UnitDateUtilsCategorydate/time routinesfunction WithinPastSeconds(const ANow, AThen: TDateTime; const ASeconds: Int64): Boolean;DescriptionCall WithinPastSeconds to determine whether the date and time specified by ANow is within ASeconds seconds of the date and time specified by AThen. Note: WithinPastSeconds uses the SecondsBetween function, which means that fractional seconds do not count. If ANow and AThen are 2 and a half seconds apart, calling WithinPastSeconds with ASeconds set to 2 returns True.
      

  2.   

    he integral part of a TDateTime value is the number of days that have passed since 12/30/1899. The fractional part of a TDateTime value is fraction of a 24 hour day that has elapsed. Following are some examples of TDateTime values and their corresponding dates and times:0 12/30/1899 12:00 am
    2.75 1/1/1900 6:00 pm
    -1.25 12/29/1899 6:00 am
    35065 1/1/1996 12:00 am
      

  3.   


    secondOf(T1)-SecondOf(T2) 返回T1和t2之是的秒数
    如:T2:=‘1999-2-3 12:34:56:78’ 
        T1:=‘1999-2-3 12:35:59:45’ 
    返回3
      

  4.   

    var
      D1,D2: TDateTime;
    begin
      if D1 >= D2 then
        result := Trunc( (D1-D2)*24*60*60 )
      else
        result := Trunc( (D2-D1)*24*60*60 ); 
    end;
      

  5.   

    Returns the number of seconds between two specified TDateTime values.UnitDateUtilsCategorydatetime routinesDelphi syntax:function SecondsBetween(const ANow, AThen: TDateTime): Int64;C++ syntax:extern PACKAGE __int64 __fastcall SecondsBetween(const System::TDateTime ANow, const System::TDateTime AThen);DescriptionCall SecondsBetween to obtain the difference, in seconds, between two TDateTime values. SecondsBetween counts only entire seconds. Thus, SecondsBetween reports the difference between 9:00:00 AM and 9:00:00:999 AM as 0 because the difference is one millisecond short of an entire second.
      

  6.   

    不难吧,如procedure TForm1.Button1Click(Sender: TObject);
    var
      Dat1,Dat2: TDatetime;
    begin
      dat1 := Strtodatetime('2005-02-02 12:11:11');
      dat2 := Strtodatetime('2001-11-22 21:43:11');
      Showmessage(Formatdatetime('yy"-""-"dd" "hh:mm:ss', dat1 - dat2));
      //你要转为秒的话,再换算一下就是了
    end;
      

  7.   

    应该用
    Function SecondSpan(ANow, AThen); Double;函数,然后用Round()函数取出上面函数的四舍五入的值就可以了。
      

  8.   

    to  madyak(无天) 
    用你的那个函数求Datetime1和Datetime2之间相差的秒数,怎么有时候少了一秒?
    规律好像是:当(t1-t2)*24*3600=***.999***时少一秒
               当(t1-t2)*24*3600=***.000***时正确
    查看函数定义为:C:\Program Files\Borland\Delphi7\Source\Rtl\Common\DateUtils.pas
    function SecondsBetween(const ANow, AThen: TDateTime): Int64;
    begin
      Result := Trunc(SecondSpan(ANow, AThen));
    end;
    错误,应该用round()取整。
      

  9.   

    uses DateUtils;SecondsBetween(ANow,AThen)