# number of days in a month

how can i get the number of days in a month?
ex.
On DECEMBER have 31 days
November have only 30 days
February (have leap year) on 28 or ..
###### Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Author Commented:
some sourcecode will do
0
Commented:
if month<12 then
days:=EncodeDate(year,month+1,1)-EncodeDate(year,month,1)
else
days:=EncodeDate(year+1,1,1)-EncodeDate(year,12,1);

F.
0
Author Commented:
I think you code is lacking and cant run with my program
0
Author Commented:
can you complete it
0
Commented:

function DaysInMonth(year:integer;month:integer):integer;
begin
if (month<1) or (month>12) then
raise Exception.Create('Invalid month in DaysInMonth call')
else if month<12 then
Result:=EncodeDate(year,month+1,1)-EncodeDate(year,month,1)
else
Result:=EncodeDate(year+1,1,1)-EncodeDate(year,12,1);
end;

F.
0
Commented:
Oh, sorry; my mistake. It should be:

function DaysInMonth(year:integer;month:integer):integer;
begin
if (month<1) or (month>12) then
raise Exception.Create('Invalid month in DaysInMonth call')
else if month<12 then
Result:=Trunc(EncodeDate(year,month+1,1)-EncodeDate(year,month,1))
else
Result:=Trunc(EncodeDate(year+1,1,1)-EncodeDate(year,12,1));
end;

0

Experts Exchange Solution brought to you by

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Commented:
procedure DaysPerMonth(Month,Year:integer):integer;
const
dmp:array[1..12] of integer=(
31,28,31,30,31,30,31,31,30,31,30,31
);
begin
Result := dmp[Month];
if Month=2 then
if Year mod 100 = 0 then
if Year mod 400 = 0 then
Inc(Result)
else
else
if Year mod 4 = 0 then
Inc(Result);
end;
0
Commented:
Hi,
This is no original, I copied from the JEDI source found in JCLDateTime.pas which has just about any kind of date/ time functions imaginable!

DaysInMonths: array [1..12] of Integer = (31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);

function DaysInMonth(const DateTime: TDateTime): Integer;
var
M: Integer;
begin
M := MonthOfDate(DateTime);
Result := DaysInMonths[M];
if M = 2 then
if IsLeapYear(DateTime) then
Result := 29;
end;

function IsLeapYear(const Year: Integer): Boolean; overload;
function IsLeapYear(const DateTime: TDateTime): Boolean; overload;
Is Leap year:  The original Gregorian rule for all who want to learn it
Result := (Year mod 4 = 0) and ((Year mod 100 <> 0) or (Year mod 400 = 0)); }

//-----------------------------------------------------------------------------

function IsLeapYear(const DateTime: TDateTime): Boolean;
begin
Result := IsLeapYear(YearOfDate(DateTime));
end;

//------------------------------------------------------------------------------

function IsLeapYear(const Year: Integer): Boolean;
begin
Result := SysUtils.IsLeapYear(Year);
end;
0
Commented:
try this :

Function GetMonthDays( Month, Year   :Integer)   :integer;
Begin
If Month = 2 Then
Begin
if Year Mod 4=0 Then
Result:= 29
Else
Result:= 28
End
Else
If Month<8 Then
Result:= 30 + Month Mod 2
Else
Result:=  31 - Month Mod 2;
End;

Regards Bakry
0
Commented:
Interesting sugestion Bakry - just one thing.  Year that end with 00 (1900, 2000, 2100 etc) will be leap years _only_ if they are evenly divisible by 400 (1600, 2000, 2400 etc)

GL
Mike
0
Author Commented:
Hey fva i just come up with other way i just

because some errors occured about the
Incompatible types ..integer and Extented ERROR

I comeup with this

function DaysInMonth(year:integer;month:integer):Integer;
var
nTemp:TdateTime;
begin
if (month<1) or (month>12) then
raise Exception.Create('Invalid month in DaysInMonth call')
else if month<12 then
nTemp:=EncodeDate(year,month+1,1)-EncodeDate(year,month,1)
else
nTemp:=EncodeDate(year+1,1,1)-EncodeDate(year,12,1);
Result:=strtoint(formatdatetime('dd',nTemp)) + 1;
end;
0
Author Commented:
Thanks for the Idea
0
Commented:
Don't mention it, but why C?

F.
0
###### It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.