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

Behaviour with GOMONTH

Status
Not open for further replies.

keepingbusy

Programmer
Apr 9, 2000
1,470
GB

Hi

Is there an explanation to this?

I have a numeric variable on a form (mforward) that allows the user to enter anything from and including 1 to 3 to show months in adavnce for a report.

The date variables are:

Code:
STORE CTOD("  /  /    ") TO mdatefrom, mdateto, mdatenext, mdateto

To increase these dates I use:

Code:
STORE GOMONTH(mdatefrom,mforward) TO mnextfrom
STORE GOMONTH(mdateto,mforward) TO mnextto

(The following are British dates)

This is ok for example with 01/01/2007 to 31/03/2007 which increases to 01/04/2007 to 30/04/2007 for 1 month or 01/05/2007 to 31/05/2007 which increases to 01/06/2007 to 30/06/2007.

However, when you use 01/02/2008 to 28/02/2007 and increase this by one month you get 01/03/2007 to 28/03/2007 when infact the latter date should 31/03/2007.

Is this a bug or something that can be written in to rectify the issue?

Thank you
Lee
 
However, when you use 01/02/2008 to 28/02/2007 and increase this by one month you get 01/03/2007 to 28/03/2007 when infact the latter date should 31/03/2007.
GOMONTH() does not return the last day of the month, BUT it return the same date in the next month. In order to get the last day of the month you need to modify you function. There are plenty of examples of that on this site, do a search on GOMONTH.

Mike Gagnon

If you want to get the best response to a question, please check out FAQ184-2483 first.
ReFox XI (www.mcrgsoftware.com)
 
Lee,

I agree with Mike G. The behaviour you described is by design. I think it's also what most people would expect the function to do.

To get the last day of the month for a given date, you could do this:

Code:
? GOMONTH(dBaseDate, 1) - DAY(GOMONTH(dBaseDate,1))

Mike


__________________________________
Mike Lewis (Edinburgh, Scotland)

My Visual FoxPro site: www.ml-consult.co.uk
 
Hi Mike (L)

Thank you for your post. I found a similar suggestion in another thread and came up with this sollution which looks at the month concerned and in particular the last day.

As my variable mnextto was storing the incorrect day (e.g. 28 instead of 31 etc), I had to break it down into day, month and year, replace the day with the correct one and put it back together as a date.

This is what I used:
Code:
STORE GOMONTH(mdatefrom,mforward)	TO mnextfrom
STORE GOMONTH(mdateto,mforward)		TO mnextto

STORE mnextto TO mdate
STORE mnextto TO mdays
mhowmany = day(gomonth(mdays, 1) - day(gomonth(mdays, 1)))
STORE STR(DAY(mdate), 2, 0) TO MDAY

IF mhowmany<10  && For dates with < 2 digits
  STORE "0"+RIGHT(mhowmany, 1) TO MDAY
ENDIF
STORE STR(MONTH(mdate), 2) TO MMONTH
IF VAL(MMONTH)<10
  STORE "0"+RIGHT(MMONTH, 1) TO MMONTH
ENDIF

STORE STR(YEAR(MDATE), 4, 0) TO MYEAR
STORE RIGHT(MYEAR, 4) TO MYEAR
STORE CTOD(STR(mhowmany,2)+"/"+MMONTH+"/"+MYEAR) TO mnextto
Thanks again to you both

Lee


Windows XP
Visual FoxPro Version 6 & 9
 
Hi Mike (Y)

Thank you for the post. I'll take a look but will probably run with what we have as this appears to be doing the job!

Kind regards

Lee


Windows XP
Visual FoxPro Version 6 & 9
 
Keepingbusy:

May be I did not understand properly the question. Why you did not use Mike Lewis's example to go to the desired mNextTo (Last day of that month) by

Code:
*
* STORE GOMONTH(mdateto,mforward)        TO mnextto
* Replacing your above statement with the following statement
*
store gomonth(mDateTo, mForward+1) - days(mDateTo, mForward+1) to mNextTo



Nasib Kalsi


 
Code:
STORE GOMONTH(mdatefrom,mforward)    TO mnextfrom
STORE GOMONTH(mdateto,mforward)        TO mnextto, mdate, mdays

STORE RIGHT("00"+ALLTRIM(STR(LastDayOfMonth(mdays))),2) TO mhowmany

STORE RIGHT("00"+ALLTRIM(STR(MONTH(mdate))), 2) TO MMONTH

STORE RIGHT("0000"+ALLTRIM(STR(YEAR(MDATE))), 4) TO MYEAR

STORE CTOD(mhowmany+"/"+MMONTH+"/"+MYEAR) TO mnextto

Mike Y: I was responding to your posted code.
I regret.

Nasib Kalsi
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top