delphi - How to calculate count of business days between two dates? -
i need calculate count of business days (working days) between 2 given dates. business days days of week except saturday , sunday. i'm not considering holidays count.
how calculate count of business days between 2 dates ?
you need utilize dayoftheweek
(from dateutils
unit) , counter, iterating through starting date ending date. (you'll need table of holidays, exclude count also.)
function businessdaysbetween(const startdate, enddate: tdatetime): integer; var currdate : tdatetime; begin currdate := startdate; result := 0; while (currdate <= enddate) begin // dayoftheweek returns 1-5 mon-fri, 6 , 7 weekends if dayoftheweek(currdate) < 6 inc(result); currdate := currdate + 1; end; end;
you can enhance little not worrying order of parameters (in other words, doesn't matter if start before end or end before start, function still work):
function businessdaysbetween(const firstdate, seconddate: tdatetime): integer; var currdate : tdatetime; startdate, enddate: tdatetime; begin if seconddate > firstdate begin startdate := firstdate; enddate := seconddate; end else begin startdate := seconddate; enddate := firstdate; end; currdate := startdate; result := 0; while (currdate <= enddate) begin if dayoftheweek(currdate) < 6 inc(result); currdate := currdate + 1; end; end;
delphi delphi-xe2
No comments:
Post a Comment