2010. július 24., szombat

Kwow if a date is end of month


Problem/Question/Abstract:

Kwow if a date is end of month

Answer:

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 IsMonthEnd( const Day: TDateTime ): boolean;
  var
    Nada, ElDia: word;
  begin
    {Hallamos el dia del mes de la fecha +1}
    {Day of month of date+1}
    DecodeDate ( Day+ 1, Nada, Nada, ElDia );
    {Si es 1, entonces es fin de mes}
    {If is 1 then is end of month}
    Result:=( ElDia=1 );
  end;

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

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 1to the date... we will obtain the following day.

Nincsenek megjegyzések:

Megjegyzés küldése