function lastmonth(ADate: TDateTime): TDateTime;
var
y, m, d: word;
begin
decodedate(adate, y, m, d);
dec(m);
if m = 0 then
begin
m := 12;
dec;
end;
try
result := encodedate(y, m, d);
except
inc(m);
if m>12 then
begin
m := 1;
inc;
end;
d := 1;
result := encodedate(y, m, d)-1;
end;
end;