DateAndTime.DatePart 方法

定義

傳回整數值,其中包含給定 Date 值的指定元件。

多載

DatePart(DateInterval, DateTime, FirstDayOfWeek, FirstWeekOfYear)

傳回整數值,其中包含給定 Date 值的指定元件。

DatePart(String, Object, FirstDayOfWeek, FirstWeekOfYear)

傳回值,其中包含給定 Date 值的指定元件。

DatePart(DateInterval, DateTime, FirstDayOfWeek, FirstWeekOfYear)

來源:
DateAndTime.vb
來源:
DateAndTime.vb
來源:
DateAndTime.vb

傳回整數值,其中包含給定 Date 值的指定元件。

public static int DatePart (Microsoft.VisualBasic.DateInterval Interval, DateTime DateValue, Microsoft.VisualBasic.FirstDayOfWeek FirstDayOfWeekValue = Microsoft.VisualBasic.FirstDayOfWeek.Sunday, Microsoft.VisualBasic.FirstWeekOfYear FirstWeekOfYearValue = Microsoft.VisualBasic.FirstWeekOfYear.Jan1);
static member DatePart : Microsoft.VisualBasic.DateInterval * DateTime * Microsoft.VisualBasic.FirstDayOfWeek * Microsoft.VisualBasic.FirstWeekOfYear -> int
Public Function DatePart (Interval As DateInterval, DateValue As DateTime, Optional FirstDayOfWeekValue As FirstDayOfWeek = Microsoft.VisualBasic.FirstDayOfWeek.Sunday, Optional FirstWeekOfYearValue As FirstWeekOfYear = Microsoft.VisualBasic.FirstWeekOfYear.Jan1) As Integer

參數

Interval
DateInterval

必要。 DateInterval 列舉值或字串運算式,表示您想要傳回的日期/時間值部分。

DateValue
DateTime

必要。 您想要評估的 Date 值。

FirstDayOfWeekValue
FirstDayOfWeek

選擇性。 選自 FirstDayOfWeek 列舉類型的值,指定一週的第一天。 如果未指定,就會使用 Sunday

FirstWeekOfYearValue
FirstWeekOfYear

選擇性。 選自 FirstWeekOfYear 列舉類型的值,指定一年的第一週。 如果未指定,就會使用 Jan1

傳回

整數值,其中包含給定 Date 值的指定元件。

例外狀況

Interval 無效。

無法將 DateValue 強制為 Date

範例

此範例會採用日期,並使用 DatePart 函式來顯示其發生年份的季。

Dim DateString, Msg As String
Dim ActualDate As Date
' Enter February 12, 2008, or 2/12/2008.
DateString = InputBox("Enter a date:")
ActualDate = CDate(DateString)

' The first two examples use enumeration values for the interval.
Msg = "Quarter: " & DatePart(DateInterval.Quarter, ActualDate)
' The quarter is 1.
MsgBox(Msg)
Msg = "The day of the month: " & DatePart(DateInterval.Day, ActualDate)
' The day of the month is 12.
MsgBox(Msg)

' The next two examples use string values for the interval parameter.
Msg = "The week of the year: " & DatePart("ww", ActualDate)
' The week of the year is 7.
MsgBox(Msg)
Msg = "The day of the week: " & DatePart("w", ActualDate)
' The day of the week is 3 (Tuesday).
MsgBox(Msg)

備註

您可以使用 函 DatePart 式來評估日期/時間值,並傳回特定的元件。 例如,您可以使用 DatePart 來計算星期數或目前的小時。

如果您選擇 DateInterval.WeekdayInterval 變數,傳回的值會與列舉的值 FirstDayOfWeek 一致。 如果您選擇 DateInterval.WeekOfYearDatePart請使用 Calendar 命名空間的 System.GlobalizationCultureInfo 類別來判斷目前的設定。

FirstDayOfWeekValue變數會影響使用 和 DateInterval.WeekOfYearInterval 設定的DateInterval.Weekday計算。 自FirstWeekOfYearValue變數會影響針對 Interval指定的DateInterval.WeekOfYear計算。

由於結構支援DateTime每個Date值,因此其方法會提供擷取日期/時間部分的額外選項。 例如,您可以取得變數的 Date 整個日期值,並將時間值設定為午夜,如下所示:

Dim CurrDatTim As Date = Now   ' Current date and time.  
Dim LastMidnight As Date = CurrDatTim.Date   ' At midnight.  

Interval 變數可以有下列其中一個設定。

列舉值 String 要傳回的日期/時間值的一部分
DateInterval.Day d 月中的日 (1 到 31)
DateInterval.DayOfYear y 年中的日 (1 到 366)
DateInterval.Hour h 小時
DateInterval.Minute n Minute
DateInterval.Month m Month
DateInterval.Quarter q
DateInterval.Second s Second
DateInterval.Weekday w 週中的日 (1 到 7)
DateInterval.WeekOfYear ww 年中的週 (1 到 53)
DateInterval.Year yyyy Year

FirstDayOfWeekValue 變數可以有下列其中一個設定。

列舉值 描述
FirstDayOfWeek.System 0 系統設定中指定的第一周第一天
FirstDayOfWeek.Sunday 1 星期日 (預設)
FirstDayOfWeek.Monday 2 星期一 (符合 ISO 標準 8601 第 3.17 節)
FirstDayOfWeek.Tuesday 3 Tuesday
FirstDayOfWeek.Wednesday 4 星期三
FirstDayOfWeek.Thursday 5 Thursday
FirstDayOfWeek.Friday 6 星期五
FirstDayOfWeek.Saturday 7 星期六

FirstWeekOfYearValue 變數可以有下列其中一個設定。

列舉值 描述
FirstWeekOfYear.System 0 系統設定中指定的第一周年份
FirstWeekOfYear.Jan1 1 1 月 1 日發生的周 (預設)
FirstWeekOfYear.FirstFourDays 2 新年至少有四天的周 (符合 ISO 標準 8601 第 3.17 節)
FirstWeekOfYear.FirstFullWeek 3 新年的第一個完整周

另請參閱

適用於

DatePart(String, Object, FirstDayOfWeek, FirstWeekOfYear)

來源:
DateAndTime.vb
來源:
DateAndTime.vb
來源:
DateAndTime.vb

傳回值,其中包含給定 Date 值的指定元件。

public static int DatePart (string Interval, object? DateValue, Microsoft.VisualBasic.FirstDayOfWeek DayOfWeek = Microsoft.VisualBasic.FirstDayOfWeek.Sunday, Microsoft.VisualBasic.FirstWeekOfYear WeekOfYear = Microsoft.VisualBasic.FirstWeekOfYear.Jan1);
public static int DatePart (string Interval, object DateValue, Microsoft.VisualBasic.FirstDayOfWeek DayOfWeek = Microsoft.VisualBasic.FirstDayOfWeek.Sunday, Microsoft.VisualBasic.FirstWeekOfYear WeekOfYear = Microsoft.VisualBasic.FirstWeekOfYear.Jan1);
static member DatePart : string * obj * Microsoft.VisualBasic.FirstDayOfWeek * Microsoft.VisualBasic.FirstWeekOfYear -> int
Public Function DatePart (Interval As String, DateValue As Object, Optional DayOfWeek As FirstDayOfWeek = Microsoft.VisualBasic.FirstDayOfWeek.Sunday, Optional WeekOfYear As FirstWeekOfYear = Microsoft.VisualBasic.FirstWeekOfYear.Jan1) As Integer

參數

Interval
String

必要。 DateInterval 列舉值或字串運算式,表示您想要傳回的日期/時間值部分。

DateValue
Object

必要。 您想要評估的 Date 值。

DayOfWeek
FirstDayOfWeek

選擇性。 選自 FirstDayOfWeek 列舉類型的值,指定一週的第一天。 如果未指定,就會使用 Sunday

WeekOfYear
FirstWeekOfYear

選擇性。 選自 FirstWeekOfYear 列舉類型的值,指定一年的第一週。 如果未指定,就會使用 Jan1

傳回

整數值,其中包含給定 Date 值的指定元件。

例外狀況

Interval 無效。

無法將 DateValue 強制為 Date

範例

此範例會採用日期,並使用 DatePart 函式來顯示其發生年份的季。

Dim DateString, Msg As String
Dim ActualDate As Date
' Enter February 12, 2008, or 2/12/2008.
DateString = InputBox("Enter a date:")
ActualDate = CDate(DateString)

' The first two examples use enumeration values for the interval.
Msg = "Quarter: " & DatePart(DateInterval.Quarter, ActualDate)
' The quarter is 1.
MsgBox(Msg)
Msg = "The day of the month: " & DatePart(DateInterval.Day, ActualDate)
' The day of the month is 12.
MsgBox(Msg)

' The next two examples use string values for the interval parameter.
Msg = "The week of the year: " & DatePart("ww", ActualDate)
' The week of the year is 7.
MsgBox(Msg)
Msg = "The day of the week: " & DatePart("w", ActualDate)
' The day of the week is 3 (Tuesday).
MsgBox(Msg)

備註

您可以使用 函 DatePart 式來評估日期/時間值,並傳回特定的元件。 例如,您可以使用 DatePart 來計算星期數或目前的小時。

如果您選擇 DateInterval.WeekdayInterval 變數,傳回的值會與列舉的值 FirstDayOfWeek 一致。 如果您選擇 DateInterval.WeekOfYearDatePart請使用 Calendar 命名空間的 System.GlobalizationCultureInfo 類別來判斷目前的設定。

FirstDayOfWeekValue變數會影響使用 和 DateInterval.WeekOfYearInterval 設定的DateInterval.Weekday計算。 自FirstWeekOfYearValue變數會影響針對 Interval指定的DateInterval.WeekOfYear計算。

由於結構支援DateTime每個Date值,因此其方法會提供擷取日期/時間部分的額外選項。 例如,您可以取得變數的 Date 整個日期值,並將時間值設定為午夜,如下所示:

Dim CurrDatTim As Date = Now   ' Current date and time.  
Dim LastMidnight As Date = CurrDatTim.Date   ' At midnight.  

Interval 變數可以有下列其中一個設定。

列舉值 String 要傳回的日期/時間值的一部分
DateInterval.Day d 月中的日 (1 到 31)
DateInterval.DayOfYear y 年中的日 (1 到 366)
DateInterval.Hour h 小時
DateInterval.Minute n Minute
DateInterval.Month m Month
DateInterval.Quarter q
DateInterval.Second s Second
DateInterval.Weekday w 週中的日 (1 到 7)
DateInterval.WeekOfYear ww 年中的週 (1 到 53)
DateInterval.Year yyyy Year

FirstDayOfWeekValue 變數可以有下列其中一個設定。

列舉值 描述
FirstDayOfWeek.System 0 系統設定中指定的第一周第一天
FirstDayOfWeek.Sunday 1 星期日 (預設)
FirstDayOfWeek.Monday 2 星期一 (符合 ISO 標準 8601 第 3.17 節)
FirstDayOfWeek.Tuesday 3 Tuesday
FirstDayOfWeek.Wednesday 4 星期三
FirstDayOfWeek.Thursday 5 Thursday
FirstDayOfWeek.Friday 6 星期五
FirstDayOfWeek.Saturday 7 星期六

FirstWeekOfYearValue 變數可以有下列其中一個設定。

列舉值 描述
FirstWeekOfYear.System 0 系統設定中指定的第一周年份
FirstWeekOfYear.Jan1 1 1 月 1 日發生的周 (預設)
FirstWeekOfYear.FirstFourDays 2 新年至少有四天的周 (符合 ISO 標準 8601 第 3.17 節)
FirstWeekOfYear.FirstFullWeek 3 新年的第一個完整周

另請參閱

適用於