在VB中,Weekday函數用于獲取指定日期的星期幾。它的用法如下:
Weekday(date[, firstdayofweek])
其中,date是要獲取星期的日期,可以是一個日期值或者一個表示日期的字符串;firstdayofweek是可選參數,用于指定每周的第一天是星期幾。
Weekday函數返回一個整數值,代表指定日期的星期幾。返回值的范圍是1到7,其中1表示星期日,2表示星期一,以此類推,7表示星期六。
以下是幾個示例:
示例1:獲取當前日期的星期幾 Dim today As Date today = Date.Today Dim weekday As Integer weekday = Weekday(today) MsgBox(“今天是星期” & weekday)
示例2:獲取指定日期的星期幾 Dim dateStr As String dateStr = “2022/01/01” Dim dateValue As Date dateValue = Date.Parse(dateStr) Dim weekday As Integer weekday = Weekday(dateValue) MsgBox(dateStr & " 是星期" & weekday)
示例3:指定每周的第一天為星期一 Dim dateStr As String dateStr = “2022/01/01” Dim dateValue As Date dateValue = Date.Parse(dateStr) Dim weekday As Integer weekday = Weekday(dateValue, vbMonday) MsgBox(dateStr & " 是星期" & weekday)
注意:在VB中,星期的起始值由系統的區域設置決定,如果沒有指定firstdayofweek參數,則默認按照系統設置來計算星期。