我想获得现在日期与原日期之差
假如数据库中取得日期为'201201010101',格式为string;
测试过程
prcedure tform1.buttonqclick(sender:tobject);
var
    tt,x:sting;
    date,dateold:tdate;
const
     t = '2012010100101';
begin
     tt :=copy(t,1,4)+'-'+copy(t,5,2)+'-'+copy(t,7,2); //tt = '2012-01-01'
     dataold := strtodate(tt);                         //出现错误,f8调试得到dateold ='49040';  
     date    := strtodate(formatdatetime('yyyy-mm-dd',now()));
     x := datetostr(date-dateold);
     showmessage(x);
end;
end.请问我上面错在哪里呢,求高人帮我修改

解决方案 »

  1.   

    这个,是打错了
    问题不在那, dataold := strtodate(tt); //结果为40909   期待为2012-01-01
      date := strtodate(formatdatetime('yyyy-mm-dd',now()));//结果为41142 期待2012-08-21
      

  2.   

    tt :=copy(t,1,4)+DateSeparator+copy(t,5,2)+DateSeparator+copy(t,7,2); date := strtodate(formatdatetime(ShortDateFormat,now()));
      

  3.   

    KK哥,看到你那个激动啊
    dateF8调试结果还是 date =41142
      

  4.   

    message结果为 1900-8-20,头疼
    我需要的结果是,x的值为 5天,40天,100天 这样的结果 
      

  5.   

    41142就是2012-08-21
    40909就是2012-01-01
    TDateTime就是Double
    没有问题。
      

  6.   

    直接把两个日期(实际上是double)相减,结果就是相差的日子。
      

  7.   


    function setdatetime(s:ansistring;var dt:TDateTime):boolean;
    var d:TDate;
        t:TTime;
        tmp:ansistring;
    begin
      Result:=false;
      if length(s)<>12 then exit;
      tmp:=copy(s,1,8);
      Insert('-',tmp,5);
      Insert('-',tmp,8);
      try
        d:=strtodate(tmp);
      except
        exit;
      end;
      tmp:=copy(s,9,4);
      Insert(':',tmp,3);
      try
        t:=strtotime(tmp);
      except
        exit;
      end;
      dt:=d+t;
      Result:=true;
    end;procedure TForm1.Button1Click(Sender: TObject);
    var d:TDateTime;
    begin
      if setdatetime('201208211148',d) then
        showmessage(formatdatetime('yyyy-mm-dd,hh:nn',d))
      else showmessage('日期时间字符串格式有误');
    end;
      

  8.   

    估计你使用的是delphi2007以上版本,如果是的话,请将你代码中的string变量类型改为AnsiString类型。
      

  9.   

    求相差天数,有一个现成的函数uses DateUtils;function DaysBetween(const ANow, AThen: TDateTime): Integer;
      

  10.   

    你用一个double来保存结果就是天数。
      

  11.   

    prcedure tform1.buttonqclick(sender:tobject);
    var tt,x:sting;
        dateold:tdate;
    const
      t = '2012010100101';
    begin
      tt :=copy(t,1,4)+'-'+copy(t,5,2)+'-'+copy(t,7,2); 
      dataold := strtodate(tt); //日期2012-01-01的值就是 49040;   
      x := date-dateold;//这里的“date”不是你原来的date,是“今天”
      showmessage('两日期差了:'+inttostr(x)+'天');
    end;
      

  12.   

    噢,变量类型未改变,勘正:
    prcedure tform1.buttonqclick(sender:tobject);
    var tt:sting;
        dateold:tdate;
        x:double;  
    const
      t = '2012010100101';
    begin
      tt :=copy(t,1,4)+'-'+copy(t,5,2)+'-'+copy(t,7,2);  
      dataold := strtodate(tt); //日期2012-01-01的值就是 49040;   
      x := date-dateold;//这里的“date”不是你原来的date,是“今天”
      showmessage('两日期差了:'+inttostr(x)+'天');
    end;
      

  13.   

    谢谢楼上各位,我已经完全理解了
    毕业很多年,因为需要才从去年年底开始自学delphi,每次热心回答问题的都是熟悉的人,感谢你们!前面我没有说的足够明白,怕说太多,影响大家耐心
    其实我是需要将数据库中最近30天的数据显示在listview里面,
    刚才两个日期相减,得到的x其实是int型,我把它理解成tdate型了,所以出错
    如此,只需 if (date - dateold) < 30 then 就ok,至此这个软件就全部完工了。
    再次感谢大家