Five day week, get prior day function
I wrote a program that requires to get the prior day, based on a five day week, so for :
Saturday..Monday, return previous Friday
Tuesday..Friday, return previous day
of course you could do it using if statements, or a case statement, but I wanted a simple and elegant, single line kinda thing, in other languages like C this would've been easier, because you can basically embed if statements in one line using ?, but in Delphi I tried different things and finally came up with this
//include DateUtils in the uses
function FiveDayWeekPriorDayEx(const theDate:TDateTime):TDateTime; const
ar:array[1..7] of Byte = (3,1,1,1,1,1,2);
begin
Result:=theDate-ar[DayOfTheWeek(theDate)]
end;
not bad, but I can probably do something with bits to come up with a shorter answer
No comments:
Post a Comment