×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
• Talk With Other Members
• Be Notified Of Responses
• Keyword Search
Favorite Forums
• Automated Signatures
• Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

#### Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

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

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

(OP)
thread184-1006090: How do I validate a date ?

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
*

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

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

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

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.

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

getting the last day of a month can be done like this:

#### CODE --> Foxpro

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

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

Hi,

Or

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

hth

MarK

#### Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

#### Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Close Box

# Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

• Talk To Other Members
• Notification Of Responses To Questions
• Favorite Forums One Click Access
• Keyword Search Of All Posts, And More...

Register now while it's still free!