Articles   Members Online:
-Article/Tip Search
-News Group Search over 21 Million news group articles.
-Delphi/Pascal
-CBuilder/C++
-C#Builder/C#
-JBuilder/Java
-Kylix
Member Area
-Home
-Account Center
-Top 10 NEW!!
-Submit Article/Tip
-Forums Upgraded!!
-My Articles
-Edit Information
-Login/Logout
-Become a Member
-Why sign up!
-Newsletter
-Chat Online!
-Indexes NEW!!
Employment
-Build your resume
-Find a job
-Post a job
-Resume Search
Contacts
-Contacts
-Feedbacks
-Link to us
-Privacy/Disclaimer
Embarcadero
Visit Embarcadero
Embarcadero Community
JEDI
Links
How to get a list of dates of specific days in a given date range Turn on/off line numbers in source code. Switch to Orginial background IDE or DSP color Comment or reply to this aritlce/tip for discussion. Bookmark this article to my favorite article(s). Print this article
13-Feb-03
Category
Algorithm
Language
Delphi 2.x
Views
118
User Rating
No Votes
# Votes
0
Replies
0
Publisher:
DSP, Administrator
Reference URL:
DKB
			Author: Tomas Rutkauskas

Can anyone help with a routine that will return a list of dates of specific days in 
a given date range? For example, I want a list of dates of the third Monday of each 
month in a given date range. The user will be able to nominate the date range, the 
day of the week, and which day (i.e. 1st, 2nd, 3rd or 4th).

Answer:

The procedure to call is ListDates(). The important function is DateInPeriod(). 
Because of DayOfWeek(), Sunday is WeekDay = 1. Tested briefly.
1   
2   function ValidateWeekDay(const WeekDay: Word): Word;
3   begin
4     Result := WeekDay mod 7;
5     if Result = 0 then
6       Result := 7;
7   end;
8   
9   function DayInMonth(const Year, Month, WeekDay, Nr: Word): Word;
10  var
11    MonthStart, Shift: Word;
12  begin
13    MonthStart := DayOfWeek(EncodeDate(Year, Month, 1));
14    Shift := ValidateWeekDay(8 + WeekDay - MonthStart);
15    Result := Shift + (7 * (Nr - 1));
16  end;
17  
18  function DateInPeriod(const Date, FromDate, ToDate: TDate): Boolean;
19  begin
20    Result := (Trunc(Date) >= Trunc(FromDate)) and (Trunc(Date) <= Trunc(ToDate))
21  end;
22  
23  procedure ListDates(const FromDate, ToDate: TDate; const WeekDay, Nr: Word;
24    const DatesList: TStrings);
25  var
26    Year, Month, Day: Word;
27    Date: TDate;
28  
29    procedure NextMonth;
30    begin
31      if Month = 12 then
32      begin
33        Month := 1;
34        inc(Year);
35      end
36      else
37        inc(Month);
38    end;
39  
40  begin
41    DatesList.Clear;
42    DecodeDate(FromDate, Year, Month, Day);
43    while EncodeDate(Year, Month, 1) <= Trunc(ToDate) do
44    begin
45      Date := EncodeDate(Year, Month, DayInMonth(Year, Month, WeekDay, Nr));
46      if DateInPeriod(Date, FromDate, ToDate) then
47        DatesList.Add(FormatDateTime(ShortDateFormat, Date));
48      NextMonth;
49    end;
50  end;


			
Vote: How useful do you find this Article/Tip?
Bad Excellent
1 2 3 4 5 6 7 8 9 10

 

Advertisement
Share this page
Advertisement
Download from Google

Copyright © Mendozi Enterprises LLC