Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Finding First Monday of Month 1

Status
Not open for further replies.

minckle

Programmer
Mar 17, 2004
142
GB
Hi im using Delphi v5, writing an app for appoinment reminders
1 typical reminder is for a Meeting on the Second Tuesday of every Month.

So how do i check if todays date is - the second tuesday of the current month???

thanks

 
Hi,

I haven't had time to put together a solution that will find the 2nd or 3rd etc occurence of a particular day, but this should get you started...

Code:
function TForm1.IsFirstDay(aDay: string): boolean;
var
  IncDate: TDate;
  Day: string;
  Today: TDate;
begin
  Today := Date;
  IncDate := StrToDate('01/'+FormatDateTime('mm', Today)+'/'+FormatDateTime('yyyy', Today));
  try
    //get first day of month that matches aDay
    repeat
      Day := FormatDateTime('dddd', IncDate);
      IncDate := IncDate+1;
      if FormatDateTime('mm', IncDate) <> FormatDateTime('mm', Today) then
        raise Exception.Create('');
    until Day = aDay;
    IncDate := IncDate-1;

    //is that day today?
    Result := (IncDate = Date);
  except
    Result := False;
    MessageDlg('End of month found without finding a match for the day!', mtError, [mbOK], 0);
  end;
end;

example of calling this:

Code:
  if IsFirstDay('Monday') then
    ShowMessage('Today''s the day!')
  else
    ShowMessage('Not today');

Steve
 
If you really only want to find if today is the second Tuesday of the month then the following should work:
Code:
function IsSecondTuesday ( dat: TDate ): boolean;
var
  d, m, y: word;
begin
  DecodeDate ( dat, y, m, d );
  result := ( DayofWeek(dat) = 3 ) and ( d in [ 8 .. 14 ] );
end;
Which could be called by something like:
Code:
  if IsSecondTuesday ( Now ) then
    ShowMessage ( 'Today is second Tuesday' )
  else
    ShowMessage ( 'Today is not second Tuesday' );

Andrew
Hampshire, UK
 
If you want a general routine that can be used for any nth day of the month then this should work:
Code:
function IsThisRequiredDay ( dat: TDate; week: integer; day: string ): boolean;
var
  dow: integer;
  y, m, d: word;
begin
  result := false;
  if LongDayNames [ DayOfWeek(dat) ] <> day then
    exit;
  DecodeDate ( dat, y, m, d );
  if ( d > week * 7 ) or ( d < ( week * 7 - 6 ) ) then
    exit;
  result := true;
end;
It should be called by something like:
Code:
if IsThisRequiredDay ( Now, 2, 'Tuesday' ) then
    ShowMessage ( 'Today is second Tuesday' )
  else
    ShowMessage ( 'Today is not second Tuesday' );

Andrew
Hampshire, UK
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top