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!

Error in Calendar

Status
Not open for further replies.

alisaif

ISP
Apr 6, 2013
418
AE
Hi,

Please refer to the subject and below image.
January is showing up to 29. Please suggest!

Thanks

Saif

calerror_molhul.png
 
Difficult to solve this problem as you haven't told us what calendar control you are using, how you have set it up, or any other information that might help us figure out what is wrong.

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads
 
Thanks for the reply!

I downloaded the control from foxite posted by Mr.Onytoo and I am using this control since long time.
One of the user intimate me about this error.

Thanks

Saif
 
Surely rather a problem of that control than of VFP itself, ie also this years January is 31 days long: ? Date(2016,02,01)-Date(2016,01,01)
If that yields 29 on your system, then it may depend on OS version or locale.

Bye, Olaf.
 
EVERY January is 31 days long.

February is 29 days long this year since it's leap year. Perhaps that is messing up this control's logic?

IAC, it's a question for the control's author.
 
I should perhaps have used the sarcasm TGML tag.
But yes, my thought also is the control is failing in this years leap day somehow.
It might be possible, that even VFP fails to compute the calendar of the OS in a arabic locale or anything like that.

Bye, Olaf.
 
Yes, the error has been fixed by changing the code as below in the control.
Code:
lnLastDay = Gomonth(.Value, 1) - .Value  && Old Code

lnLastDay = Day(Gomonth(Date(m.lnYear,m.lnMonth,1), 1) - 1) && Change with

Thanks for the time sharing with me.

Saif
 
Makes sense, as the old formula is valid for most days of a month, but not the ones at the end. GOMONTH has the nature to transform an end of a month into the end of the next month. If the next month is shorter, it does not go the current month length forward, but rather the next month length, which is what happened here and put the february length to the january length.

So if .Value was for example 31st January 2016 (as per your screenshot), you get GOMONTH(Date(2016,1,31),1)-Date(2016,1,31), which is 29 instead of 31. This error is not only occuring in leap years, it occurs with every 31 day date in a month with a shorter follow up month, and there are many transitions of months with 31 days to next months with 30 days or less only.

Bye, Olaf.
 
Here are some other examples.

This ought to work for going forward or backward to the first day of the month:
Code:
nMoveMonths = 1  && Positive=Future; 0= this month; Negative=Past
? GOMONTH(DATE(),nMoveMonths) - (DAY(DATE()) - 1)

This ought to work for going forward or backward to the last day of the month:
Code:
nMoveMonths = 1  && Positive=Future; 0= this month; Negative=Past
? GOMONTH(DATE(),nMoveMonths + 1) - (DAY(DATE()))
 
No dbMark,

You have the same problem again, DAY(DATE()) can be 31 while GOMONTH(DATE(),x) can move to day 30 or even day 28 and then subtracting 31 goes wrong.

The correct expression Saif used explicitly takes the 1st of the current month before GOMONTH, so this always ends at the first of another month and you don't have the possible shift of 1 or 2 days for DATE() at the end of a month introduced by GOMONTH(DATE(),x), simply test it for GOMONTH(Date(2016,1,31),1), that is 02/29/2016, now subtracting DAY(DATE()-1) means subtracting 30 and that goes back to 1/31/2106 instead of 2/1/2016.

You better look up working expressions for such calculations, eg the fox wiki also has formulas for first/last day of months:
~ dDate-DAY(dDate)+1
~ GOMONTH(dDate-Day(dDate)+1,1)-1

expanding that in your parameterization would be doing
First Day of some Month (0=current month):
GOMONTH(dDate-DAY(dDate)+1,nMoveMonth)

Last Day of some Month (0=current month):
GOMONTH(dDate-DAY(dDate)+1,nMoveMonth+1)-1

Bye, Olaf.
 
Olaf, you're right. It worked for the current date I was testing on but not always on others. I wasn't thorough and hereby retract that code. Thanks!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top