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

Return the first or last day of the month

Usefull Functions & Procedures

Return the first or last day of the month

by  StewartUK  Posted    (Edited  )
This was developed from thread184-566780 with thanks to Andrew Coates for the syntax for the last day of the month.

Obviously myDate is a variable of type DATE. You could replace this with the VFP DATE() for the current date.

You could easily put these into a function feeding in mydate and constants (say 1 to get the first day & 31 to get the last day) to determine whether the function returns the first day of the month or the last day. You could have a third parameter for stating whether to return dates for the previous, current or next month say -1, 0 & 1.

I've tested these functions with first, middle & last dates of the next 12 months (which includes a leap-year) and they seemed to return the correct results in every case.

For the first day of the previous month
[color blue]GOMONTH(GOMONTH(myDate, IIF(DAY(mydate)=1,1,0)) - DAY(GOMONTH(myDate, IIF(DAY(mydate)=1,1,0))-1),-1)[/color]
For the last day of the previous month
[color blue]GOMONTH(myDate, 0) - DAY(GOMONTH(myDate, 0))[/color]

For the first day of the current month
[color blue]GOMONTH(myDate, IIF(DAY(mydate)=1,1,0)) - DAY(GOMONTH(myDate, IIF(DAY(mydate)=1,1,0))-1)[/color]
For the last day of the current month
[color blue]GOMONTH(myDate, 1) - DAY(GOMONTH(myDate, 1))[/color]

For the first day of the next month
[color blue]GOMONTH(GOMONTH(myDate, IIF(DAY(mydate)=1,1,0)) - DAY(GOMONTH(myDate, IIF(DAY(mydate)=1,1,0))-1),1)[/color]
For the last day of the next month
[color blue]GOMONTH(mydate,2)-DAY(GOMONTH(mydate,2))[/color]

Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top