我想获得现在日期与原日期之差
假如数据库中取得日期为'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.请问我上面错在哪里呢,求高人帮我修改
假如数据库中取得日期为'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.请问我上面错在哪里呢,求高人帮我修改
问题不在那, dataold := strtodate(tt); //结果为40909 期待为2012-01-01
date := strtodate(formatdatetime('yyyy-mm-dd',now()));//结果为41142 期待2012-08-21
dateF8调试结果还是 date =41142
我需要的结果是,x的值为 5天,40天,100天 这样的结果
40909就是2012-01-01
TDateTime就是Double
没有问题。
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;
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;
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;
毕业很多年,因为需要才从去年年底开始自学delphi,每次热心回答问题的都是熟悉的人,感谢你们!前面我没有说的足够明白,怕说太多,影响大家耐心
其实我是需要将数据库中最近30天的数据显示在listview里面,
刚才两个日期相减,得到的x其实是int型,我把它理解成tdate型了,所以出错
如此,只需 if (date - dateold) < 30 then 就ok,至此这个软件就全部完工了。
再次感谢大家