VB6 WeekdayName Function
The WeekdayName function returns a string indicating the specified day of the week.
Syntax
WeekdayName(weekday[, abbreviate[, firstdayofweek]])
Parameters
weekday: Required. The numeric designation for the day of the week. Numeric value of each day depends on thefirstdayofweeksetting.abbreviate: Optional. Boolean value that indicates if the weekday name is to be abbreviated. If omitted, the default is False (not abbreviated).firstdayofweek: Optional. Numeric value indicating the first day of the week. See Settings section for values.
FirstDayOfWeek Constants
vbUseSystemDayOfWeek(0): Use National Language Support (NLS) API settingvbSunday(1): Sunday (default)vbMonday(2): MondayvbTuesday(3): TuesdayvbWednesday(4): WednesdayvbThursday(5): ThursdayvbFriday(6): FridayvbSaturday(7): Saturday
Returns
Returns a String containing the name of the specified day of the week. The string is localized based on the system's regional settings.
Remarks
The WeekdayName function provides localized day names:
- Localization: Returns day names according to system locale (e.g., "Monday" in English, "Lundi" in French)
- Abbreviation: When
abbreviateis True, returns shortened form (e.g., "Mon" instead of "Monday") - Weekday parameter: Numeric value from 1 to 7
- Default first day: Sunday (vbSunday = 1) if
firstdayofweeknot specified - Consistency with Weekday: Use same
firstdayofweekvalue as Weekday function for consistency - System locale: Output language depends on Windows regional settings
- Case sensitivity: Returned string typically has proper capitalization
- Abbreviation length: Typically 3 characters in English, varies by locale
Understanding Weekday Parameter
The weekday parameter's meaning depends on firstdayofweek:
- If firstdayofweek is vbSunday (default): 1=Sunday, 2=Monday, 3=Tuesday, etc.
- If firstdayofweek is vbMonday: 1=Monday, 2=Tuesday, 3=Wednesday, etc.
- The number always refers to the position in the week, starting from the specified first day
Abbreviation Examples (English locale)
- Full: "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"
- Abbreviated: "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
Combining with Weekday Function
' Get the name of today's day
dayName = WeekdayName(Weekday(Date))
Typical Uses
- Display Day Names: Show user-friendly day names in UI
- Report Headers: Label columns or sections with day names
- Calendar Applications: Display calendar grid headers
- Localized Applications: Provide day names in user's language
- Schedule Display: Show schedule with day names
- Date Formatting: Create custom date format strings
- Dropdown Lists: Populate day selection dropdowns
- Log Files: Human-readable date information in logs
Basic Examples
Example 1: Get Today's Day Name
Sub ShowTodayName()
Dim todayName As String
todayName = WeekdayName(Weekday(Date))
MsgBox "Today is " & todayName
End Sub
Example 2: Display All Day Names
Sub ListAllDays()
Dim i As Integer
For i = 1 To 7
Debug.Print WeekdayName(i)
Next i
End Sub
Example 3: Abbreviated Day Names
Function GetAbbreviatedDayName(dayNumber As Integer) As String
GetAbbreviatedDayName = WeekdayName(dayNumber, True)
End Function
' Usage:
Debug.Print GetAbbreviatedDayName(1) ' Prints "Sun" (if vbSunday is first day)
Example 4: Create Calendar Header
Function CreateCalendarHeader(Optional abbreviated As Boolean = True) As String
Dim i As Integer
Dim header As String
header = ""
For i = 1 To 7
header = header & WeekdayName(i, abbreviated, vbSunday) & vbTab
Next i
CreateCalendarHeader = header
End Function
Common Patterns
Pattern 1: Format Date with Day Name
Function FormatDateWithDayName(dt As Date) As String
FormatDateWithDayName = WeekdayName(Weekday(dt)) & ", " & Format$(dt, "mmmm d, yyyy")
End Function
Pattern 2: Get All Day Names Array
Function GetDayNamesArray(Optional abbreviated As Boolean = False) As String()
Dim days(1 To 7) As String
Dim i As Integer
For i = 1 To 7
days(i) = WeekdayName(i, abbreviated)
Next i
GetDayNamesArray = days
End Function
Pattern 3: ISO Week Day Names (Monday First)
Function GetISODayName(dayNumber As Integer, Optional abbreviated As Boolean = False) As String
GetISODayName = WeekdayName(dayNumber, abbreviated, vbMonday)
End Function
Pattern 4: Populate ComboBox with Days
Sub PopulateDayCombo(combo As ComboBox)
Dim i As Integer
combo.Clear
For i = 1 To 7
combo.AddItem WeekdayName(i)
Next i
End Sub
Pattern 5: Get Day Initials
Function GetDayInitial(dayNumber As Integer) As String
GetDayInitial = Left$(WeekdayName(dayNumber, True), 1)
End Function
Pattern 6: Create Week Schedule Header
Function CreateWeekSchedule() As String
Dim i As Integer
Dim schedule As String
schedule = "Week Schedule:" & vbCrLf
For i = 1 To 7
schedule = schedule & WeekdayName(i) & ": _____" & vbCrLf
Next i
CreateWeekSchedule = schedule
End Function
Pattern 7: Format Event Description
Function FormatEventDescription(eventDate As Date, eventName As String) As String
FormatEventDescription = eventName & " on " & _
WeekdayName(Weekday(eventDate)) & ", " & _
Format$(eventDate, "mmmm d")
End Function
Pattern 8: Get Weekday vs Weekend Label
Function GetDayTypeLabel(dt As Date) As String
Dim dayNum As Integer
dayNum = Weekday(dt)
If dayNum = vbSaturday Or dayNum = vbSunday Then
GetDayTypeLabel = WeekdayName(dayNum) & " (Weekend)"
Else
GetDayTypeLabel = WeekdayName(dayNum) & " (Weekday)"
End If
End Function
Pattern 9: Create Pivot Headers
Function CreatePivotDayHeaders() As Variant
Dim headers(1 To 7) As String
Dim i As Integer
For i = 1 To 7
headers(i) = WeekdayName(i, True, vbMonday)
Next i
CreatePivotDayHeaders = headers
End Function
Pattern 10: Conditional Day Name Display
Function GetDisplayDayName(dt As Date, useAbbreviation As Boolean) As String
GetDisplayDayName = WeekdayName(Weekday(dt), useAbbreviation)
End Function
Advanced Usage
Example 1: Calendar Header Generator Class
' Class: CalendarHeaderGenerator
' Generates calendar headers with configurable options
Option Explicit
Private m_FirstDayOfWeek As VbDayOfWeek
Private m_Abbreviated As Boolean
Private m_Separator As String
Public Sub Initialize(Optional firstDay As VbDayOfWeek = vbSunday, _
Optional abbreviated As Boolean = True, _
Optional separator As String = " ")
m_FirstDayOfWeek = firstDay
m_Abbreviated = abbreviated
m_Separator = separator
End Sub
Public Function GenerateHeader() As String
Dim i As Integer
Dim header As String
header = ""
For i = 1 To 7
If i > 1 Then header = header & m_Separator
header = header & WeekdayName(i, m_Abbreviated, m_FirstDayOfWeek)
Next i
GenerateHeader = header
End Function
Public Function GenerateHeaderArray() As String()
Dim headers(1 To 7) As String
Dim i As Integer
For i = 1 To 7
headers(i) = WeekdayName(i, m_Abbreviated, m_FirstDayOfWeek)
Next i
GenerateHeaderArray = headers
End Function
Public Function GetDayName(dayNumber As Integer) As String
If dayNumber < 1 Or dayNumber > 7 Then
Err.Raise 5, , "Day number must be between 1 and 7"
End If
GetDayName = WeekdayName(dayNumber, m_Abbreviated, m_FirstDayOfWeek)
End Function
Example 2: Date Formatter Module
' Module: DateFormatter
' Advanced date formatting with day names
Option Explicit
Public Function FormatLongDate(dt As Date) As String
FormatLongDate = WeekdayName(Weekday(dt)) & ", " & _
Format$(dt, "mmmm d, yyyy")
End Function
Public Function FormatShortDate(dt As Date) As String
FormatShortDate = WeekdayName(Weekday(dt), True) & " " & _
Format$(dt, "mm/dd/yyyy")
End Function
Public Function FormatScheduleDate(dt As Date) As String
FormatScheduleDate = WeekdayName(Weekday(dt), True) & ", " & _
Format$(dt, "mmm d")
End Function
Public Function FormatCalendarDate(dt As Date) As String
FormatCalendarDate = WeekdayName(Weekday(dt)) & vbCrLf & _
Format$(dt, "d")
End Function
Public Function GetDayWithOrdinal(dt As Date) As String
Dim dayNum As Integer
Dim suffix As String
dayNum = Day(dt)
Select Case dayNum
Case 1, 21, 31
suffix = "st"
Case 2, 22
suffix = "nd"
Case 3, 23
suffix = "rd"
Case Else
suffix = "th"
End Select
GetDayWithOrdinal = WeekdayName(Weekday(dt)) & ", " & _
MonthName(Month(dt)) & " " & dayNum & suffix
End Function
Example 3: Schedule Analyzer Class
' Class: ScheduleAnalyzer
' Analyzes schedules and provides day-based insights
Option Explicit
Public Function GetDayDistributionReport(dates() As Date) As String
Dim dayCounts(1 To 7) As Integer
Dim i As Long
Dim report As String
Dim dayNum As Integer
' Count occurrences
For i = LBound(dates) To UBound(dates)
dayNum = Weekday(dates(i))
dayCounts(dayNum) = dayCounts(dayNum) + 1
Next i
' Build report
report = "Day Distribution:" & vbCrLf
For i = 1 To 7
report = report & WeekdayName(i) & ": " & dayCounts(i) & vbCrLf
Next i
GetDayDistributionReport = report
End Function
Public Function GetMostCommonDay(dates() As Date) As String
Dim dayCounts(1 To 7) As Integer
Dim i As Long
Dim maxCount As Integer
Dim maxDay As Integer
Dim dayNum As Integer
For i = LBound(dates) To UBound(dates)
dayNum = Weekday(dates(i))
dayCounts(dayNum) = dayCounts(dayNum) + 1
Next i
maxCount = 0
maxDay = 1
For i = 1 To 7
If dayCounts(i) > maxCount Then
maxCount = dayCounts(i)
maxDay = i
End If
Next i
GetMostCommonDay = WeekdayName(maxDay)
End Function
Public Function CreateDaySummary(dates() As Date) As Collection
Dim summary As New Collection
Dim i As Integer
For i = 1 To 7
summary.Add 0, WeekdayName(i)
Next i
Dim dt As Variant
For Each dt In dates
Dim dayName As String
dayName = WeekdayName(Weekday(dt))
summary.Remove dayName
summary.Add summary(dayName) + 1, dayName
Next dt
Set CreateDaySummary = summary
End Function
Example 4: Localization Helper Module
' Module: LocalizationHelper
' Helps with localized day name handling
Option Explicit
Public Function GetLocalizedDayNames(Optional abbreviated As Boolean = False, _
Optional firstDay As VbDayOfWeek = vbSunday) As String()
Dim names(1 To 7) As String
Dim i As Integer
For i = 1 To 7
names(i) = WeekdayName(i, abbreviated, firstDay)
Next i
GetLocalizedDayNames = names
End Function
Public Function CreateDayNameLookup(Optional abbreviated As Boolean = False) As Collection
Dim lookup As New Collection
Dim i As Integer
For i = 1 To 7
lookup.Add WeekdayName(i, abbreviated), CStr(i)
Next i
Set CreateDayNameLookup = lookup
End Function
Public Function FindDayNumber(dayName As String) As Integer
Dim i As Integer
For i = 1 To 7
If UCase$(WeekdayName(i)) = UCase$(dayName) Then
FindDayNumber = i
Exit Function
End If
If UCase$(WeekdayName(i, True)) = UCase$(dayName) Then
FindDayNumber = i
Exit Function
End If
Next i
FindDayNumber = 0 ' Not found
End Function
Public Function IsDayNameValid(dayName As String) As Boolean
IsDayNameValid = (FindDayNumber(dayName) > 0)
End Function
Public Function NormalizeDayName(dayName As String) As String
Dim dayNum As Integer
dayNum = FindDayNumber(dayName)
If dayNum > 0 Then
NormalizeDayName = WeekdayName(dayNum)
Else
NormalizeDayName = ""
End If
End Function
Error Handling
The WeekdayName function can raise the following errors:
- Error 5 (Invalid procedure call or argument): If
weekdayis less than 1 or greater than 7 - Error 5 (Invalid procedure call or argument): If
firstdayofweekis not between 0 and 7 - Error 13 (Type mismatch): If arguments are not of correct type
Performance Notes
- Very fast operation - simple lookup/formatting
- Constant time O(1) complexity
- No significant performance difference between abbreviated and full forms
- Can be called repeatedly without performance concerns
- Consider caching day name arrays if used frequently in loops
Best Practices
- Use consistent firstdayofweek with Weekday function to avoid confusion
- Cache day name arrays if populating lists or grids repeatedly
- Handle localization - day names will differ based on system locale
- Document expectations about which day is first in week
- Use abbreviation parameter for space-constrained displays
- Validate weekday range (1-7) before calling
- Consider
MonthNamefor consistent date formatting patterns - Test with different locales if application is internationalized
- Use named constants (vbMonday, etc.) for clarity
- Combine with Format$ for custom date displays
Comparison Table
| Function | Purpose | Returns | Localized |
|---|---|---|---|
WeekdayName |
Get day name | String | Yes |
Weekday |
Get day number | Integer (1-7) | No |
MonthName |
Get month name | String | Yes |
Format$ |
Format date | String | Partially |
Platform Notes
- Available in VB6, VBA, and
VBScript - Behavior consistent across platforms
- Output localized based on system regional settings
- Abbreviation format varies by locale
- English (US): "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
- Other languages will return appropriate translations
Limitations
- Cannot customize day name output (uses system locale)
- Abbreviation length not configurable (determined by locale)
- No way to get day name in specific language (uses system setting)
- Cannot get day names for custom calendars (e.g., Hebrew, Islamic)
- No built-in way to get single-letter day abbreviations
- Cannot specify case (capitalization) of returned string