Icon View Thread

The following is the text of the current message along with any replies.
Messages 1 to 2 of 2 total
Thread Date Oops
Fri, Mar 13 2015 10:44 AMPermanent Link

Matthew Jones

I've got a problem with dates:

var
tmToday := Date;
tmDateTime : DateTime;
begin

tmDateTime := // Some routine that gets a date

if DayOf(tmDateTime) > DayOf(JulToDate(DateToJul(tmToday) - 5)) then
 // sort of works, but oops!

Now, this code is supposed to see if the date is within the last 5
days, and if so, show a friendly "Thursday" instead of "2015-03-03" or
something. Which it does, but only for recent dates. Dates last month
are now being shown as Saturday too, which is wrong! Turns out the
DayOf() function is the day of the month.

What is the best way to make this "over 5 days ago"? The DateTime type
is just milliseconds, so is it really a matter of subtracting 5 *
86,400,000 from the value? I guess so, but would quite like a nicer way.

--

Matthew Jones
Fri, Mar 13 2015 10:55 AMPermanent Link

Matthew Jones

Matthew Jones wrote:

> What is the best way to make this "over 5 days ago"? The DateTime type
> is just milliseconds, so is it really a matter of subtracting 5 *
> 86,400,000 from the value? I guess so, but would quite like a nicer
> way.

This seems to work well enough:

function DaysSince(tmEventDate : DateTime) : Integer;
var
   nMilliSecsPerDay : Integer;
   tmToday : DateTime;
begin
   tmToday := Date;
   nMilliSecsPerDay := 86400000;                  

   Result := ((tmToday - tmEventDate) div nMilliSecsPerDay) + 1;
end;

--

Matthew Jones
Image