Home

Finding the Name of the Week for a Date

 

Introduction

Sometimes when you get a date in a record, you may way to know the weekday name of that date. The Visual Basic language provides the DatePart() function that allows you to know the numeric index of the day. For example, the days are represented as:

  1. Sunday

  2. Monday

  3. Tuesday

  4. Wednesday

  5. Thursday

  6. Friday

  7. Saturday

Using this sequence, to get the string name of a weekday, you can create an array of strings that holds the names of weekdays, call the DatePart() function and pass it the date value. Imagine you have a text box named txtDateHired and another named txtWeekday. In the following example, when the first text box loses focus, the event retrieves the date value of 
the first text box, finds its weekday and displays it in the second text box:

Private Sub txtDateHired_LostFocus()
	Dim dteDateHired As Date
	Dim DaysNames(7) As String

	DaysNames(0) = ""
	DaysNames(1) = "Sunday"
	DaysNames(2) = "Monday"
	DaysNames(3) = "Tuesday"
	DaysNames(4) = "Wednesday"
	DaysNames(5) = "Thursday"
	DaysNames(6) = "Friday"
	DaysNames(7) = "Saturday"

	dteDateHired = CDate([txtDateHired])
	[txtWeekday] = DaysNames(DatePart("w", dteDateHired))
End Sub

 

Home Copyright © 2005-2016, FunctionX