Weekday Function (Visual Basic)
Returns an Integer value containing a number representing the day of the week.
Public Function Weekday( _
ByVal DateValue As DateTime, _
Optional ByVal DayOfWeek As FirstDayOfWeek = FirstDayOfWeek.Sunday _
) As Integer
Parameters
DateValue
Required. Date value for which you want to determine the day of the week.DayOfWeek
Optional. A value chosen from the FirstDayOfWeek enumeration that specifies the first day of the week. If not specified, FirstDayOfWeek.Sunday is used.
Settings
The DayOfWeek argument can have one of the following settings.
Enumeration value |
Value |
Description |
---|---|---|
FirstDayOfWeek.System |
0 |
First day of week specified in system settings |
FirstDayOfWeek.Sunday |
1 |
Sunday (default) |
FirstDayOfWeek.Monday |
2 |
Monday (complies with ISO standard 8601, section 3.17) |
FirstDayOfWeek.Tuesday |
3 |
Tuesday |
FirstDayOfWeek.Wednesday |
4 |
Wednesday |
FirstDayOfWeek.Thursday |
5 |
Thursday |
FirstDayOfWeek.Friday |
6 |
Friday |
FirstDayOfWeek.Saturday |
7 |
Saturday |
Exceptions
Exception type |
Error number |
Condition |
---|---|---|
DayOfWeek is less than 0 or more than 7. |
See the "Error number" column if you are upgrading Visual Basic 6.0 applications that use unstructured error handling. (You can compare the error number against the Number Property (Err Object).) However, when possible, you should consider replacing such error control with Structured Exception Handling Overview for Visual Basic.
Remarks
The value returned by the Weekday function is the day of the week that the DateValue corresponds to, relative to the first day of the week defined by the DayOfWeek value. For example, if the date refers to a Wednesday and the first day of the week is specified as Monday, the function returns 3.
Note
Weekday uses the current calendar setting from the CurrentCulture property of the CultureInfo class in the System.Globalization namespace. The default CurrentCulture values are determined by Control Panel settings.
Example
The following example uses the Weekday function to obtain the day of the week from a specified date.
Dim oldDate As Date
Dim oldWeekDay As Integer
oldDate = #2/12/1969#
oldWeekDay = Weekday(oldDate)
' oldWeekDay now contains 4 because thisDate represents a Wednesday.
Requirements
Namespace: Microsoft.VisualBasic
Module: DateAndTime
Assembly: Visual Basic Runtime Library (in Microsoft.VisualBasic.dll)
See Also
Reference
WeekdayName Function (Visual Basic)