Mega Code Archive

 
Categories / Delphi / Examples
 

Days in month

This article demonstrates how to detect how many days there are in a given month. procedure TForm1.Button1Click(Sender: TObject); var MyDate : TDateTime; tmpStr : String; tmpInt : Integer; begin MyDate := Date() + 365 - (30*5); tmpStr := FormatDateTime('mmmm yyyy',MyDate); tmpInt := DaysInMonth(MyDate); ShowMessage(tmpStr + ' has ' + IntToStr(tmpInt) + ' days...'); end; function TForm1.DaysInMonth(ADate:TDateTime):Integer; var MyMonth, MyYear, MyDay : Word; MyDayTable : TDayTable; tmpBool : Boolean; begin DecodeDate(ADate, MyYear, MyMonth, MyDay); tmpBool := IsLeapYear(MyYear); MyDayTable := MonthDays[tmpBool]; Result := MyDayTable[MyMonth]; end;