Visit our Sponsor   Visit our Sponsor
delphi3000.com - the free delphi knowledge platform
delphi3000.com - the free delphi knowledge platform
Have a look at your member-status

connecting people's knowledge


  - Recent ArticlesRSS feed for Recent Articles on delphi3000.com
  - List of All Articles
  - Top Viewed Articles
  - Articles (+Attachem.)
  - Articles Of Interest
  - Categories
  - Top Uploader
  - Search
  - Index

  - My Home
  - Submit an Article
  - My Articles
  - My Personal Data
  - My Bookmarks
  - Activities
  - Login/Logout

  - Sign Up
  - Why Sign Up
  - Newsletter

  - Press
  - Advertise

  - Contact
  - Feedback





Community
Borland
ClubeDelphi
Dr. Bob
UK-BUG
Delphi Meetings
Planeta Delphi







Startblatt.de






Share this article with friendsShare this article with friends
Rate this articleRate this article - to keep the quality of delphi3000.com !
Comment this article or read through previous comments (0)


Next or Prev working DayFormat this article printer-friendly!Bookmark function is only available for registered users!
and skip holidays
Product:
Delphi all versions
Category:
VCL-General
Skill Level:
Scoring:
Last Update:
05/16/2003
Search Keys:
delphi delphi3000 article borland vcl code-snippet day date DayOfWeek next callback weekend holiday
Times Scored:
2
Visits:
2169
Uploader: Andreas Schmidt
Company:
Reference: N/A
 
Question/Problem/Abstract:
How to calculate the next/prev working day starting from a given date.
working days are monday to friday.
This article replaces the obsolete artice 2387
Answer:



type
  // callback function for holidays
  THolidayCallback = function(const d:TDateTime):boolean;


function PrevWorkingDay(const date:TDateTime; callback:THolidayCallback):TDateTime;
begin
   Result := Int(date) - 1.0;  // yesterday 0:00

   if Assigned(callback) then
      while (DayOfWeek(Result) in [1,7]) or callback(Result) do
         Result := Result - 1.0
   else
      while (DayOfWeek(Result) in [1,7]) do
         Result := Result - 1.0;
end;

function NextWorkingDay(const date:TDateTime; callback:THolidayCallback):TDateTime;
begin
   Result := Int(date) + 1.0;  // tomorrow 0:00

   if Assigned(callback) then
      while (DayOfWeek(Result) in [1,7]) or callback(Result) do
         Result := Result + 1.0
   else
      while (DayOfWeek(Result) in [1,7]) do
         Result := Result + 1.0;
end;

===========================================================================

// example for the callback function

function IsHoliday(const d:TDateTime):boolean;
var
   year, month, day;
begin
   DecodeDate(d, year, month, day);
   if (month = 12) and ((day=25) or (day=26)) then
      result := True   // christmas
   else if (month=12) and (day=31) then
      result := True   // sylvester
   else if (month=1) (day=1) then
      result := True;

   // more queries here
   // e.g. a lookup in a database
end;





Please rate this article!
Skill level:
BeginnerExpert

Useful:
No!Very!

Overall rating:
PoorExcellent



Comments to this article
Write a new comment













 
Sign up to consume product discounts for Bronze memberships !

read more


  Visit our Sponsor

 

  Community Ad of
C.A. Longen
 
   














 







     
  Copyright © 2000 - 2007 delphi3000.com - All rights reserved. Terms of use. || Privacy
delphi3000.com is a service by bluestep.com IT-Services GmbH (Vienna)