Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to count days in a month ?

Status
Not open for further replies.

viettla

Programmer
Jul 19, 2002
34
VN
Hi experts,
I'm would like count days in a month I look at internal function of VFP but there is no function like it. Could anyone tell me about algorithm or API function which can do that ?
thank you very much !

Viet
 

Viet,

Do you mean that, given a month and year number, you want to return the number of days in that month? If so, something like this might do it:

Code:
x = DATE(yr, mnth, 1)  && yr = year, mnth = month
y = GOMONTH(x,1)
answer = y - x

If that's not what you want, could you be more specific.

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

My sites:
Visual FoxPro (www.ml-consult.demon.co.uk)
Crystal Reports (www.ml-crystal.com)
 

Doing a search in the VFP forums you would this for example :

Code:
?DaysInmOnth(DATE())
Function DaysInMonth
    Parameter mDate
    Private mDate,mDays,mDateFmt
    mDateFmt = Set("Date")
    set Date to British
    mDays = Day(Ctod("1/"+alltrim(str(Month(mDate+32),2,0))+"/"+alltrim(str(Year(mDate+32),4,0)))-1)
    
    Set Date to (mDateFmt)
        
return(mDays)



Mike Gagnon

If you want to get the best response to a question, please check out FAQ184-2483 first.
ReFox XI (www.mcrgsoftware.com)
 
Hi Viet,

foxpro offers all the basic date functions you need to calculate anything regarding dates, as both Mikes showed.

The question can be rephrased to "what is the last date of the month?". The number of days then simply is
Code:
DAY(lastdayofmonth).

As finding the last day is a more common problem, here are some more ways to find the last day:
And another one regarding a calculation based on a date as single parameter:
Code:
? LastDayOfMonth(Date())

Function LastDayOfMonth(tdDate)
   Local ldDate
   ldDate = tdDate+28
   Return ldDate-DAY(ldDate)
Endif

simply adding DAY() you get the days of a month with:

Code:
? MonthDays(Date())

Function MonthDays(tdDate)
   Local ldDate
   ldDate = tdDate+28
   Return DAY(ldDate-DAY(ldDate))
Endif

Bye, Olaf.
 
Hi again,

sorry, made a mistake: a date+28 must not be within the next month, so replace tdDate+28 with GoMonth(tdDate,1), that will always work correctly.

Bye, Olaf.
 
Thanks to all for solutions. I'm sorry when my question is not clear, as Mike showed I need to count number of days in current month.
Your help was highly appreciated.

Viet
 
Try this also,

*--- declaring variables
Local LnDIM1,LnDIM2,LnDIM3,LnDIM4,LnDIM5,LnDIM6,LnDIM7,LnDIM8,;
LnDIM9,LnDIM10,LnDIM11,LnDIM12

Local ldStart1,ldStart2,ldStart3,ldStart4,ldStart5,ldStart6,ldStart7,ldStart8,;
ldStart9,ldStart10,ldStart11,ldStart12

Local ldend1,ldend2,ldend3,ldend4,ldend5,ldend6,ldend7,ldend8,;
ldend9,ldend10,ldend11,ldend12

Clear
*-- calculating how many dates have months
LnDIM1 = Ctod( "^" + Alltrim(Str(Year(Date()))) + "/01/01")
LnDIM2 = Ctod( "^" + Alltrim(Str(Year(Date()))) + "/02/01")
LnDIM3 = Ctod( "^" + Alltrim(Str(Year(Date()))) + "/03/01")
LnDIM4 = Ctod( "^" + Alltrim(Str(Year(Date()))) + "/04/01")
LnDIM5 = Ctod( "^" + Alltrim(Str(Year(Date()))) + "/05/01")
LnDIM6 = Ctod( "^" + Alltrim(Str(Year(Date()))) + "/06/01")
LnDIM7 = Ctod( "^" + Alltrim(Str(Year(Date()))) + "/07/01")
LnDIM8 = Ctod( "^" + Alltrim(Str(Year(Date()))) + "/08/01")
LnDIM9 = Ctod( "^" + Alltrim(Str(Year(Date()))) + "/09/01")
LnDIM10 = Ctod( "^" + Alltrim(Str(Year(Date()))) + "/10/01")
LnDIM11 = Ctod( "^" + Alltrim(Str(Year(Date()))) + "/11/01")
LnDIM12 = Ctod( "^" + Alltrim(Str(Year(Date()))) + "/12/01")


? DaysInMonth(LnDIM1)
? DaysInMonth(LnDIM2)
? DaysInMonth(LnDIM3)
? DaysInMonth(LnDIM4)
? DaysInMonth(LnDIM5)
? DaysInMonth(LnDIM6)
? DaysInMonth(LnDIM7)
? DaysInMonth(LnDIM8)
? DaysInMonth(LnDIM9)
? DaysInMonth(LnDIM10)
? DaysInMonth(LnDIM11)
? DaysInMonth(LnDIM12)



*----- start date of each month
ldStart1 = Ctod( "^" + Alltrim(Str(Year(Date()))) + "/01/01")
ldStart2 = Ctod( "^" + Alltrim(Str(Year(Date()))) + "/02/01")
ldStart3 = Ctod( "^" + Alltrim(Str(Year(Date()))) + "/03/01")
ldStart4 = Ctod( "^" + Alltrim(Str(Year(Date()))) + "/04/01")
ldStart5 = Ctod( "^" + Alltrim(Str(Year(Date()))) + "/05/01")
ldStart6 = Ctod( "^" + Alltrim(Str(Year(Date()))) + "/06/01")
ldStart7 = Ctod( "^" + Alltrim(Str(Year(Date()))) + "/07/01")
ldStart8 = Ctod( "^" + Alltrim(Str(Year(Date()))) + "/08/01")
ldStart9 = Ctod( "^" + Alltrim(Str(Year(Date()))) + "/09/01")
ldStart10 = Ctod( "^" + Alltrim(Str(Year(Date()))) + "/10/01")
ldStart11 = Ctod( "^" + Alltrim(Str(Year(Date()))) + "/11/01")
ldStart12 = Ctod( "^" + Alltrim(Str(Year(Date()))) + "/12/01")

*--- end date of each month
ldEnd1 = ldStart1 + DaysInMonth(LnDIM1)-1
ldEnd2 = ldStart2 + DaysInMonth(LnDIM2)-1
ldEnd3 = ldStart3 + DaysInMonth(LnDIM3)-1
ldEnd4 = ldStart4 + DaysInMonth(LnDIM4)-1
ldEnd5 = ldStart5 + DaysInMonth(LnDIM5)-1
ldEnd6 = ldStart6 + DaysInMonth(LnDIM6)-1
ldEnd7 = ldStart7 + DaysInMonth(LnDIM7)-1
ldEnd8 = ldStart8 + DaysInMonth(LnDIM8)-1
ldEnd9 = ldStart9 + DaysInMonth(LnDIM9)-1
ldEnd10 =ldStart10 + DaysInMonth(LnDIM10)-1
ldEnd11 =ldStart11 + DaysInMonth(LnDIM11)-1
ldEnd12 =ldStart12 + DaysInMonth(LnDIM12)-1


? ldStart1
? ldStart2
? ldStart3
? ldStart4
? ldStart5
? ldStart6
? ldStart7
? ldStart8
? ldStart9
? ldStart10
? ldStart11
? ldStart12

? ldEnd1
? ldEnd2
? ldEnd3
? ldEnd4
? ldEnd5
? ldEnd6
? ldEnd7
? ldEnd8
? ldEnd9
? ldEnd10
? ldEnd11
? ldEnd12

*--------------------------------------------------------------
Function DaysInMonth
Lparameters p_dDate && any valid day in month interested in

ldFirstOfMonth = Date(Year(p_dDate), Month(p_dDate), 1)
ldFirstOfNextMonth = Gomonth(ldFirstOfMonth, 1)
Return (ldFirstOfNextMonth - ldFirstOfMonth)

Soykan OEZCELIK
 

Here is a general way of finding number of days in month where YEAR and MONTH are known:

DaysInMonth = 30 + ABS(MONTH % 2 - INT(MONTH/8)) - IIF(MONTH = 2, 2, 0) + IIF(YEAR % 4 = 0, 1, 0) - IIF(YEAR % 400 = 0, 1, 0)

I just concocted this up in the last 20 minutes. I am sure that with some study that this can be consolidated even more.



mmerlinn

"Political correctness is the BADGE of a COWARD!"

 
A quick and dirty...

Code:
? DaysInMonth(date())            && Current date
? DaysInMonth(date(2004,10,10))  && A specific date
? DaysInMonth(ctod("2/21/2000")) && Leap month

function DaysInMonth(dDate)
  local dMonthPlus1
  dMonthPlus1 = gomonth(date(year(dDate),month(dDate),1),1)
  return dMonthPlus1 - date(year(dDate),month(dDate),1)
endfunc

Darrell
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top