"STOP, Welopez! You've been making me work my poor fingers to the bone for the past hour! What in the world are you doing?"
He was sitting atop my mouse, a dimunitive character, almost microscopic. In his left hand he held a sort of long stick with dozens of beads on it. "Eh? Who are you?"
"I'm Artie Abacii, the Worble in charge of your math co-processor. I do all the calculations for your programs. Now, why have you been working me so hard with all those FOR/NEXT loops?"
"I'm writing a routine to calculate the day of the week for any given date," I told him. "Since JB does not have a native Weekday$() function, I have to calculate it each time, and take into account Leap Years and Leap Centuries. Boy, these calendar routines are extremely complicated."
"That's because you are making it complicated, Welopez. When Carl released JB 1.01, he added the MOD function, which makes it very easy for you to determine any weekday, past, present, or future."
"He did? What is a MOD function?"
"The MOD function returns the ramainder of num1 when divided by num2. In other words, 38 MOD 5 = 3. 38 divided by 5 equals 7, with a remainder of 3. Got that? The only value which we want is the remainder. We used to use several lines of code and an INT value to get the remainder, but the MOD function will return that to you very easily. Kapeesh?"
"Of course I do, Artie. I can do arithmetic, you know? So how will this MOD function help me?"
"In the routine you are using, Welopez, you know what the present day of the week is, so you are counting forward or backward to find the day of week for a given date. The MOD function eliminates all the counting."
"How?" Really, I had been working on that problem for awhile, but since FOR/NEXT routines are so easy, that's what I had been doing.
"Look, before I became an independant contractor, I spent quite some time working for Microsoft and teaching those gnomes in Redmon how to do things. Many of the things I do in your math co-processor are based upon routines I worked out for MS with my abacus." He began waving that silly stick thing at me.
"That doesn't look like any abacus, suan pan or soroban I've ever seen, Artie."
"Because my abacus is binary, dummy."
"Oh." I should have known that.
"When you type DATE$("mm/dd/yyyy") into your program, Welopez, you are getting the number of days, not a string but a real number, since Jan 1, 1901, from the system clock. Because I was there when this was set up, I can assure you, Jan 1, 1901 was a Tuesday."
"You were born in 1901?" I asked, incredulously.
"Don't be ridiculous, Welopez. I just chose that date as a reference point, then calculated the day of week only once. Because I know 01/01/1901 was a Tuesday, I can get any other day of the week by dividing the number of days between DATE$() and the search$ MOD 7. The remainder will tell me what the day of week is for the search$. Enter this code into your IDE and run it. Choose several different dates and check them against your calendar if you don't believe me.
'Day of Week using MOD function in a GOSUB
[new]
CLS
INPUT "Choose a date to search for: (mm/dd/yyyy) "; search$
GOSUB [dayOfWk]
IF VAL(RIGHT$(search$,4)) < VAL(RIGHT$(DATE$(),4)) THEN
PRINT search$; " was on a "; day$; "."
ELSE
PRINT search$; " will be on a "; day$; "."
END IF
INPUT "Do another? (Y/N) "; resp$
IF resp$="" THEN [quit]
resp$=UPPER$(resp$)
IF LEFT$(resp$,1)="Y" THEN [new]
WAIT
[quit]
END
[dayOfWk]
weekday=DATE$(search$)MOD 7 'weekday = the ramainder when search$ is divided by 7
SELECT CASE weekday
CASE 1
day$="Wednesday"
CASE 2
day$="Thursday"
CASE 3
day$="Friday"
CASE 4
day$="Saturday"
CASE 5
day$="Sunday"
CASE 6
day$="Monday"
CASE 0
day$="Tuesday"
END SELECT
RETURN
"Hey, that is really slick," I told Artie. "But why did we have to use the yyyy format? Wouldn't it be faster if we just entered yy?".
"It might be faster for you, Welopez, but when I have to do the comparison to determine whether the date you're searching for lies in the future or in the past, how am I supposed to know if you mean 2005 or 1905?"
"Oh, I see..." I said, wiping egg off my face. Of course he was correct, that's what that whole Y2K schmear was about.
"Now that you know how to determine the day of week using simple arithmetic, Welopez, please copy and paste this GOSUB to the next program where you need it. All you have to do is INPUT a date as search$ and the GOSUB will get the day of week for you. Now, if you'll excuse me, your wife is calling me to calculate interest on her credit card. I think she's planning on spending more money, buddy. While I'm helping her, here's a little digital clock routine to keep you amused."
'Clock in Text Window
TIMER 250, [UpdateClock] 'The timer can be anywhere in the program.
NOMAINWIN
WindowWidth = 200
WindowHeight = 125
UpperLeftX=int((DisplayWidth-WindowWidth)/2)
UpperLeftY=int((DisplayHeight-WindowHeight)/2)
TEXTBOX #main.textbox1, 60, 5, 120, 25
TEXTBOX #main.textbox2, 60, 35, 120, 25
TEXTBOX #main.textbox3, 60, 65, 120, 25
STATICTEXT #main.statictext4, "Day", 10, 10, 40, 25
STATICTEXT #main.statictext5, "Time", 10, 40, 40, 25
STATICTEXT #main.statictext6, "Date", 10, 67, 40, 25
OPEN "Good day to you!" FOR WINDOW AS #main
PRINT #main, "trapclose [quit.main]"
PRINT #main, "font arial 12 bold"
[UpdateClock]
valueTime$ = time$()
IF VAL(LEFT$(time$,2))<12 THEN
valueTime$=valueTime$;" p.m."
ELSE
valueTime$=valueTime$;" a.m."
END IF
valueDate$ = date$()
GOSUB [dayOfWk]
PRINT #main.textbox1, day$
PRINT #main.textbox2, valueTime$
PRINT #main.textbox3, valueDate$
WAIT
[quit.main] 'End the program
TIMER 0
CLOSE #main
END
[dayOfWk]
day=DATE$("days")MOD 7
SELECT CASE day
CASE 1
day$="Wednesday"
CASE 2
day$="Thursday"
CASE 3
day$="Friday"
CASE 4
day$="Saturday"
CASE 5
day$="Sunday"
CASE 6
day$="Monday"
CASE 0
day$="Tuesday"
END SELECT
RETURN
I didn't really need Artie's digital clock, I can always use the one in the notification area of my system tray, but I did play around with Artie's day of week routine while checking my almanac to see if he had made a mistake somewhere. Apparently it works fine for any year, leap year or leap century. Now I don't have to code all those FOR/NEXT loops and IF/THEN comparisons again, the MOD function easily does that for me.
Thanks, Carl! (And thanks to Artie too!)
And next time, get the sign right, Welopez! Arti