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!

Calculate days in the month.... 3

Status
Not open for further replies.

stlrain95

Programmer
Sep 21, 2001
224
US
I am looking for a way to get the number of days in the month using FoxPro and a little code. i.e. 30,31,28....

If I store the date before I run a program I can use the month to get the days, but just not sure on how to perform this?

Suggestions.

This is what I use to store the end of the month date.

STORE date() - day(date()): This gives me 09/30/2002, I need to know how many days in that month?

thanks,
 
This will do it.

Code:
myDate = DATE()
SET DATE DMY && You can change the function to your settings
?GOMONTH(CTOD("01"+alltrim(SUBSTR(DTOC(myDate),3,8))),1)-1
myChoppedUpDate=GOMONTH(CTOD("01"+alltrim(SUBSTR(DTOC(myDate),3,8))),1)-1
?SUBSTR(DTOC(myChoppedUpDate),1,2)
Mike Gagnon

If you want to get the best response to a question, please check out FAQ184-2483 first
 
This compensates for leap years.

I'm sure someone will come up with something more elegant.

Darrell

p.s. You'll need to test for a valid date input

? DaysInMonth(ctod("02/22/2002"))
* Return 28

? DaysInMonth(ctod("02/29/2000"))
* Returns 29

? DaysInMonth(ctod("01/22/2002"))
* Returns 31


function DaysInMonth(ldDate)

local lcMon, lcYear, lnDay
* Pull month and year from date
lcMon = str(month(ldDate))
lcYear = str(year(ldDate))
for lnDay = 31 to 28 step -1
if day(ctod(lcMon+&quot;/&quot;+str(lnDay)+&quot;/&quot;+lcYear)) <> 0
return lnDay
endif
next
endfunc 'We all must do the hard bits so when we get bit we know where to bite' :)
 
After looking at Mike's code, I'll add that if you use my code you'll have to compensate for your regional date setting.

Darrell 'We all must do the hard bits so when we get bit we know where to bite' :)
 
HI

Pass any date type variable to this function, you will get back a bumerical value equal to number of days in the month.
You dont have to worry about SET DATE reginal settings.

FUNCTION DaysOfMonth
LPARAMETERS lDate
IF TYPE(&quot;lDATE&quot;) = &quot;D&quot;
RETURN DAY(GOMONTH((lDATE-day(lDATE)+1),1)-1)
ENDIF

:) ramani :)
(Subramanian.G),FoxAcc, ramani_g@yahoo.com
 
to get the last day of the month

ldLastDay = GOMONTH(date(), 1) - day(date())

10/31/02 Attitude is Everything
 
HI danceman..

Try your sample with 30th or 31st Jan of any year ?
ramani :)
(Subramanian.G),FoxAcc, ramani_g@yahoo.com
 
Well, it looks like you have plenty of suggestions. But why not 1 more ...

Here is a function where you pass it the month and year and it returns the number of days in that month without worrying about leap years, etc ... which is exactly what you asked for.


function DaysThisMonth(nMonth,nYear)
local nDays,cMonth,cYear,cStartDate,dTheDate
cYear = str(nYear,4)
cMonth = alltrim(str(nMonth,2,0))
if len(cMonth) = 1
cMonth = &quot;0&quot;+cMonth
endif
cStartDate = cMonth + &quot;/01/&quot;+cYear
dTheDate = ctod(cStartDate)
nDays = 0
do while month(dTheDate) = nMonth
nDays = nDays + 1
dTheDate = dTheDate + 1
enddo
return nDays






Don
dond@csrinc.com

 
Ramani got my star for the most elegant.

I almost gave Mike one - I may still - but I noticed the anomaly also. Who can think of something useful to do with the anomaly?

Darrell
'We all must do the hard bits so when we get bit we know where to bite' :)
 
zmnth = MONTH(DATE())
zyear = YEAR(DATE())
qstdate = CTOD(&quot;01/&quot;+zmnth+&quot;/&quot;+zyear)
qendate = GOMONTH(qstdate,1)-1
? DAY(qendate)
 
zmnth = STR(MONTH(DATE()))
zyear = STR(YEAR(DATE()))
qstdate = CTOD(&quot;01/&quot;+zmnth+&quot;/&quot;+zyear)
qendate = GOMONTH(qstdate,1)-1
? DAY(qendate)
 
Bada

zmnth = STR(MONTH(DATE()))
zyear = STR(YEAR(DATE()))
qstdate = CTOD(&quot;01/&quot;+zmnth+&quot;/&quot;+zyear)
qendate = GOMONTH(qstdate,1)-1
? DAY(qendate)

qstdate = CTOD(&quot;01/&quot;+zmnth+&quot;/&quot;+zyear)

You cannot add numeric values (zmnth and zyear) and character values (&quot;01&quot;)together . Mike Gagnon

If you want to get the best response to a question, please check out FAQ184-2483 first
 
mgagnon

zmnth = STR(MONTH(DATE()))
zyear = STR(YEAR(DATE()))

zmnth & zyear are not numeric they are converted using str function.
Bada
 
Bada

zmnth = MONTH(DATE())
zyear = YEAR(DATE())
qstdate = CTOD(&quot;01/&quot;+zmnth+&quot;/&quot;+zyear)
qendate = GOMONTH(qstdate,1)-1
? DAY(qendate)


Sorry, I didn't notice you had corrected your function. Next time you may to state that your are correcting your function.



Mike Gagnon

If you want to get the best response to a question, please check out FAQ184-2483 first
 
*/***************************************************************************
*/Program : function MLD
*/System :
*/Purpose : Returns the last day of the month specified.
*/Syntax : L_Date = mld(date())
*/Returns : date - last day of month
*/Parameter : date - any date in the month
*/Defaults : Nothing
*/Requires : Nothing
*/Changes : Nothing
*/Calls :
*/Version : 1.0
*/Dated :
*/Written By: David W. Grewe
*/***************************************************************************
*& Date Function
*/***************************************************************************
*/ Record Of Change
*/
*/***************************************************************************
parameters P_DATE
private P_DATE
if empty(P_DATE)
P_DATE = date()
endif
*
* add the difference between P_DATE's day and 32 to obtain the
* next month, then subtract that date's day to obtain the last
* day of the month of month(P_DATE)
*
return P_DATE+(32-DAY(P_DATE))-day(P_DATE+(32-DAY(P_DATE)))
David W. Grewe
Dave@internationalbid.com
 
mdate=date() &&any date

?DAY(CTOD(STR(month(mdate)+1,2)+&quot;/01/&quot;+STR(YEAR(DATE()),4))-1)
 
Well, here's mine too:

STORE CTOD('02/01/2000') TO mydate
HowMany = day(gomonth(mydate, 1) - day(gomonth(mydate, 1)))
Dave S.
 
by the way, my doesnt work :)

correct will be:

IIF(MONTH(mdate)<12,DAY(CTOD(STR(month(mdate)+1,2)+&quot;/01/&quot;+STR(YEAR(mdate),4))-1),DAY(CTOD(&quot;01/01/&quot;+STR(YEAR(mdate)+1,4))-1))

but your way is much much better.
 
Being REAL old I rememberd a great set of date UDFs from the classic Tom Rettig Foxpro Handbook. So here is what the late master would have said......

here is the original code... i think that one statement in the iif(between(year,0,99...... would need to be modified to reflect the fact that we are now past 1999...


anyway.... thought it was a kick to see a classic!


*action: evaluates the last day of the month
*syntax: lastday(<N month> [, <N year>])
*return: <expN> last day of the month
*Notes: returns zero if the month parameter is not between * 1 and 12. If the optional year is not specified, the *current system -date year is assumed. If a one or two *digit year is specified the current system date century is *assumed.
* Usage: To get the last day of the current month use; lastday(month(date()))
* To discover if a year is a leap year use: lastday(2,<year>)==29
*
*

function lastday
paramteres month, year
private yr

if !between(month,1,12)
return 0
endif

if parameters()==2
yr = iif(between(year,0,99),;
ltrim(str(int(year(date())/100)*100+year,4)),;
ltrim(str(year,4)))
else
yr = ltrim(str(year(date()),4))
endif

return day(gomonth({1/31/&yr}, month-1))

*
*
*disclaimer.... I used these way back when.... they worked fine... again...just a kick real late on a friday night...enjoy!



Tom Gahagan
edrest@alltel.net

REST



If you get a chance to sit out or dance...

I hope you dance. L Wommack
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top