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

How to validate a date - last day of ANY Month in any Year

Status
Not open for further replies.

atamotua

IS-IT--Management
Jan 24, 2011
3
US
thread184-1006090

How to validate a date - last day of ANY Month in any Year
Here's a small effective function that will do it.

*
* FUNC Get_Last_Day_Of_Month
*
* Start at day 31 and keep going down/back in days til we find the last good day of this month

LPARAMETERS pdDate && pass in a date you know is valid, with a valid MONTH and YEAR, like "01/01/1999"
TRY
pdDate = CTOD(pdDate) && in case we send in a date as a string - in string format
CATCH
ENDTRY
nMonth = MONTH(pdDate)
nYear = YEAR(pdDate)

* Compute LAST DAY OF MONTH that was passed in
dNewDate = CTOD(' / / ')
nDay = 31
DO WHILE EMPTY(dNewDate) && when Date is invalid, date HighDate will be empty
dNewDate = CTOD(ALLTRIM(TRANSFORM(nMonth)) + '/' + TRANSFORM(nDay) + '/' + ALLTRIM(TRANSFORM(nYear)))
nDay = nDay - 1
ENDDO

RETURN dNewDate
*
* ENDFUNC
*
 
I'm not exactly sure what you're trying to do, but there are more efficient ways to find the last day of a month. Assuming you're given a date, to find the last day of that month, you can do this:

Code:
LPARAMETERS pdDate

LOCAL ldLastDay

ldLastDay = GOMONTH(m.pdDate - DAY(m.pdDate) + 1, 1) - 1

RETURN m.ldLastDay

This finds the first of the month, then goes forward a month, and then subtracts one day.

Tamar
 
Besides, the old thread you referred to wasn't at all about determining the last day of a month. It was about verifying a date string is a valid date.

Bye, Olaf.
 
getting the last day of a month can be done like this:

Code:
CLEAR 

?GetLastDayOfMonth( DATE() )
?GetLastDayOfMonth( {12.02.2012} )

FUNCTION GetLastDayOfMonth as Date 
LPARAMETERS vDate as Date

    RETURN DATE( YEAR( GOMONTH( vDate , 1 ) ) , MONTH( GOMONTH( vDate , 1 ) ) , 1 ) - 1

ENDFUNC

HTH

-Tom
 
Hi,

Or

? GoMonth(dDate, 1) - Day(GoMonth(dDate, 1))

hth

MarK
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top