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 Rhinorhino on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Number of days into a month 1

Status
Not open for further replies.

pmscraiova

Programmer
Joined
Dec 8, 2001
Messages
86
Location
RO
I want to find out the number of days into a month. For exemple, how many days are in february this year (but next year?). Sorry for my english...
Thank you, Eugen Fintina
 
This seems to work:
Code:
*FUNCTION DatesinMonth
LPARAMETERS p_nYear, p_nMonth
LOCAL ldDate
IF PCOUNT() < 2 OR VARTYPE(p_nMonth) <> &quot;N&quot;
	p_nMonth = MONTH(DATE())
ENDIF
IF PCOUNT() < 1 OR VARTYPE(p_nYear) <> &quot;N&quot;
	p_nYear = YEAR(DATE())
ENDIF
ldDate = DATE(p_nYear, p_nMonth, 1)

RETURN (GOMONTH(ldDate,1)-ldDate)

examples:
?daysinmonth()
?daysinmonth(2002)
?daysinmonth(2000,2)
?daysinmonth(2001,2)
?daysinmonth(1900,2)

Rick

 
Rick, thank you very much. It works...
 
I just use:

day({03/01/2002}-1)

note {03/01/2002} I just use the first day of the next month minus 1 this will always return the last day of any month and will work for leap year and any other date business, even a small function that takes month,year would be

function daysofmonth
parameter n_month,n_year

rvalue=day(ctod(alltrim(str(n_month+1))+'/01/'+alltrim(str(n_year)))-1)

note we increase the passed month by one and always use the note first then -1 works every time a slight vareation
note would return the date of the end of the month for
note report dates that need to know the end of month date

return (rvalue)
endfunc
Steve Bowman
steve.bowman@wayservices.com

 
Steve,
Doesn't this fail for Decembers?

Rick
 
you are correct, you would have to check for 12 then increase n_year by 1 and change n_month to 1

or just always return 31 if n_month=12
Thanks

function daysofmonth
parameter n_month,n_year

if n_month=12
n_year=n_year+1
n_month=1
endif

rvalue=day(ctod(alltrim(str(n_month+1))+'/01/'+alltrim(str(n_year)))-1)


return (rvalue)
endfunc

thanks
Steve Bowman
steve.bowman@wayservices.com

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top