Solved

number of days in a month

Posted on 2001-06-02
13
218 Views
Last Modified: 2010-04-06
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 ..
0
Comment
Question by:ryan_sabarre
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
13 Comments
 

Author Comment

by:ryan_sabarre
ID: 6148596
some sourcecode will do
0
 
LVL 4

Expert Comment

by:fva
ID: 6148635
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 Comment

by:ryan_sabarre
ID: 6148689
I think you code is lacking and cant run with my program
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:ryan_sabarre
ID: 6148691
can you complete it
0
 
LVL 4

Expert Comment

by:fva
ID: 6148738

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
 
LVL 4

Accepted Solution

by:
fva earned 50 total points
ID: 6148739
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
 
LVL 4

Expert Comment

by:DMN
ID: 6148769
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
 
LVL 1

Expert Comment

by:martin_g
ID: 6148863
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;

{Don't forget to add overload to the 2 IsLeapYear functions below in your declaration section
 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
 
LVL 1

Expert Comment

by:bakry99
ID: 6148975
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
 
LVL 6

Expert Comment

by:edey
ID: 6149022
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 Comment

by:ryan_sabarre
ID: 6149050
Hey fva i just come up with other way i just
change something about your source.

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 Comment

by:ryan_sabarre
ID: 6149054
Thanks for the Idea
0
 
LVL 4

Expert Comment

by:fva
ID: 6151519
Don't mention it, but why C?

F.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Run video youtube webbrowse 10 86
Why can't I create any project with my delphi EXE file? 6 58
Dynamically Created Query 3 80
Automatic field translation delphi 10.2 6 45
A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

738 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question