Browse DevX
Sign up for e-mail newsletters from DevX

Tip of the Day
Language: Visual Basic
Expertise: Advanced
Dec 22, 1998



Building the Right Environment to Support AI, Machine Learning and Deep Learning

A Quicker "Next/Previous Weekday"

In the latest tips supplement ["101 Tech Tips for VB Developers," Supplement to the February 1998 issue of VBPJ], I noticed a tip titled "Determine Next/Previous Weekday." This code is shorter and accomplishes the same task with no DLL calls:
Public Function SpecificWeekday(ByVal D As Date, Optional _
	ByVal WhatDay As VbDayOfWeek = vbSaturday, _
	Optional GetNext As Boolean = True) As Date 
	SpecificWeekday = (((D - WhatDay + GetNext) \ 7) - _
		GetNext) * 7 + WhatDay
End Function
This code averages about 10 times faster in VB3 and up to 30 times faster in VB5. It works because VB keeps dates internally as the number of days since Saturday, December 30, 1899. A date of 1 represents Sunday, December 31, 1899, which is also its own weekday. This means the WeekDay function is equivalent to the expression (Date - 1) Mod 7 + 1. This is coded for VB5, but by altering the way the Optional parameters are handled, you can make it work in either VB3 or VB4.
Phil Parsons
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date