在delphi中我想用一下系统时间减去另一个时间怎么写啊?谢谢?

解决方案 »

  1.   

    var 
      d : TDateTime;
      days : integer;
    begin
      d := StrToDate('2003-1-1');
      days := Date() - d;
      ShowMessage(Format('相差%d天',[days]));
    end;
      

  2.   

    var 
    t,t1, t2: TDateTime; 
    begin
    t1:=now;
    t = TDateTime(t1 - t2)
    end; 
    然后用DecodeTime分解时间
      

  3.   

    我要与系统时间比较的时间是我从query中查出来的日期型字段。怎么写啊?
    谢谢!!
      

  4.   

    var 
    t,t1, t2: TDateTime; 
    begin
    t1:=now;
    t2:=query1.fieldbyname('datetime').asdatetime;
    t = TDateTime(t1 - t2)
    end; 
    然后用DecodeTime分解时间
      

  5.   

    你告诉我的不对t = TDateTime(t1 - t2)?
      

  6.   

    var nYear, nMonth, nDay: Word;
        oYear, oMonth, oDay: Word;
    begin  DecodeDate(query1.fieldbyname('查询时间').AsDateTime,oYear, oMonth, oDay);
      DecodeDate(now,nYear, nMonth, nDay);
      n:=nyear-oyear;//相差的年数  m:=n*12+nyear-oyear;//相差的月数  d:=n*365+m*30+nday-oday;//相差的天数(这里只是举个例子,相差的天数这样不对的)
      

  7.   

    var nYear, nMonth, nDay: Word;
        oYear, oMonth, oDay: Word;
        m,n,d:Integer;
    begin  DecodeDate(query1.fieldbyname('查询时间').AsDateTime,oYear, oMonth, oDay);
      DecodeDate(now,nYear, nMonth, nDay);
      n:=nyear-oyear;//相差的年数  m:=n*12+nyear-oyear;//相差的月数  d:=VarToInt(now-query1.fieldbyname('查询时间').AsDateTime);//相差的天数可以这样
                                                                 //得到
      

  8.   

    to zwhu
       您的我照您写的在WIN2000+D6下试了一下,
       其中 VARTOINT函数是怎么得来的?我查了DELPHI的帮助,没有这个函数啊.
      

  9.   

    不好意思,可以:StrToInt(VarToInt(now-query1.fieldbyname('查询时间').AsDateTime));相差的秒数可以:
    procedure TForm1.Button1Click(Sender: TObject);
    var nYear, nMonth, nDay: Word;
        oYear, oMonth, oDay: Word;
        m,n,d:Integer;
        nHour, nMin, nSec, nMSec: Word;
        oHour, oMin, oSec, oMSec: Word;
        s:String;//要的秒数
    begin
      DecodeDate(strtodatetime('2004-01-29'),oYear, oMonth, oDay);
      DecodeDate(now,nYear, nMonth, nDay);
      n:=nyear-oyear;//相差的年数
      m:=n*12+nyear-oyear;//相差的月数
      d:=StrToInt(VarToStr(date-strtodate('2004-01-29')));  decodetime(Now,nHour,nMin,nSec,nMSec);
      decodetime(strtodatetime('2004-01-28'),oHour,oMin,oSec,oMSec);
      s:=vartostr(d*24*3600+(nHour-oHour)*3600+(nMin-oMin)*60+nSec-oSec);
      showmessage(s);
    end;以上调试在win2000+delphi6通过,你只要把'2004-01-28'修改成你的数据库查询出来的时间即可
      

  10.   

    var nYear, nMonth, nDay: Word;
        oYear, oMonth, oDay: Word;
        m,n,d:Integer;
        nHour, nMin, nSec, nMSec: Word;
        oHour, oMin, oSec, oMSec: Word;
        s:String;//要的秒数
    begin
      DecodeDate(query1.FieldByName('所查询时间').AsDateTime,oYear, oMonth, oDay);
      DecodeDate(now,nYear, nMonth, nDay);
      n:=nyear-oyear;//相差的年数
      m:=n*12+nyear-oyear;//相差的月数
      d:=StrToInt(VarToStr(date-query1.FieldByName('所查询时间').AsDate));  decodetime(Now,nHour,nMin,nSec,nMSec);
      decodetime(query1.FieldByName('所查询时间').AsDateTime,oHour,oMin,oSec,oMSec);
      s:=vartostr(d*24*3600+(nHour-oHour)*3600+(nMin-oMin)*60+nSec-oSec);
      showmessage(s);写的匆忙,呵。。
      

  11.   

    zwhu(心灵琴音) 你好!!!
    你看看一下我的(? 小弟有问题请大哥们帮忙!!谢谢!! (lzd1024 ))这个帖子
    你是高手。帮帮老弟。谢谢了!!!!!!
      

  12.   

    我看了,应该是sql语句少了逗号了吧,呵,终于发现有人和我一样粗心了,不知道是不是哦