Know if a date is end of month  

Send By: Radikal (Q3 Team)
Web :
Date: 24/08/01

Tip accessed 547 times


It's to say: know if a date is the last day of its month

 procedure TForm1.Button1Click(Sender: TObject);

   {Devuelve TRUE si la fecha dada es el ultimo dia del mes
   Returns TRUE if the date is the last day of the month}
   function EsFinDeMes( const QueDia: TDateTime ): boolean;
     Nada, ElDia: word;
     {Hallamos el dia del mes de la fecha +1}
     {Day of month of date+1}
     DecodeDate ( QueDia + 1, Nada, Nada, ElDia );
     {Si es 1, entonces es fin de mes}
     {If is 1 then is end of month}
     Result:=( ElDia=1 );

   {Ejemplo de llamada:}
   {A call Example:}
   if EsFinDeMes(Now) then ShowMessage( 'Hoy es fin de mes!+
                                        'Today is end of the month!');

The operation is as simple as to make a DecodeDate of the date + 1, this way we will obtain the following day to which we are inspecting; if it is day 1... it means that the day in question is month end.
Let us don't forget that the format TDateTime that Delphi uses uses the whole part to score the days lapsed from 12/30/1899, so if we added him a 1 to the date... we will obtain the following day.