Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Language: Visual Basic
Expertise: Intermediate
Aug 24, 1998

Determine Next/Previous Weekday

This function returns week identifier information, such as Week Beginning/Ending date, and makes it more generic to return a date of any previous or following weekday. You can use this function in VB5 and VBA5; with minor modifications to the optional arguments, you can also use it in VB3 and VB4:

 Public Function SpecificWeekday(d As Date, _
	Optional ByVal WhatDay As Byte = _
	vbSaturday, Optional ByVal GetNext _
	As Boolean = True) As Date
	'Purpose: Returns a date of the 
	'Next/Previous specific weekday for a given date
	' Input:		d - Date for which next/previous 
	'			weekday is needed 
	'			WhatDay - optional (byte) 
	'			argument (default = vbSaturday)
	'			specifying what weekday to find
	'			GetNext - optional (Boolean) 
	'			argument: True for 
	'			Next(default), False for Previous
	'Output:		Date of the next/previous specific weekday
	'Note:		If both optional arguments are 
	'			omitted, returns Week-Ending Date
	'			If passed date (d) falls on the 
	'			same weekday as WhatDay, the 
	'			function returns the passed date 
	'			unchanged.
	Dim iAdd As Integer
	iAdd = (WhatDay - WeekDay(d))
	If GetNext Then
		SpecificWeekday = DateAdd("d", IIf(_
			iAdd >= 0, iAdd, 7 + iAdd), d)
	Else
		SpecificWeekday = DateAdd("d", IIf(_
			iAdd <= 0, iAdd, iAdd - 7), d)
	End If
End Function
Serge Natanov
 
Comment and Contribute

 

 

 

 

 


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

 

 

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