DateAndTime.DatePart 方法

定义

返回包含给定 Date 值的指定组件的整数值。Returns an integer value containing the specified component of a given Date value.

重载

DatePart(DateInterval, DateTime, FirstDayOfWeek, FirstWeekOfYear)

返回包含给定 Date 值的指定组件的整数值。Returns an integer value containing the specified component of a given Date value.

DatePart(String, Object, FirstDayOfWeek, FirstWeekOfYear)

返回一个值,其中包含给定 Date 值的指定组件。Returns an value containing the specified component of a given Date value.

DatePart(DateInterval, DateTime, FirstDayOfWeek, FirstWeekOfYear)

返回包含给定 Date 值的指定组件的整数值。Returns an integer value containing the specified component of a given Date value.

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

必需。Required. DateInterval 枚举值或字符串表达式,它表示要返回的日期/时间值部分。A DateInterval enumeration value or a string expression representing the part of the date/time value you want to return.

DateValue
DateTime

必需。Required. 要计算的 Date 值。The Date value that you want to evaluate.

FirstDayOfWeekValue
FirstDayOfWeek

可选。Optional. FirstDayOfWeek 枚举中选择的值,用于指定一周的第一天。A value chosen from the FirstDayOfWeek enumeration that specifies the first day of the week. 如果未指定,则使用 SundayIf not specified, Sunday is used.

FirstWeekOfYearValue
FirstWeekOfYear

可选。Optional. FirstWeekOfYear 枚举中选择的值,用于指定一年的第一周。A value chosen from the FirstWeekOfYear enumeration that specifies the first week of the year. 如果未指定,则使用 Jan1If not specified, Jan1 is used.

返回

包含给定 Date 值的指定组件的整数值。An integer value containing the specified component of a given Date value.

异常

Interval 无效。Interval is not valid.

DateValue 并不强制为 DateDateValue is not coercible to Date.

示例

此示例使用了一个日期,并使用DatePart函数来显示该日期在一年中的哪个季度。This example takes a date and, using the DatePart function, displays the quarter of the year in which it occurs.

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函数来评估日期/时间值并返回特定的组件。You can use the DatePart function to evaluate a date/time value and return a specific component. 例如,你可以使用DatePart来计算一周中的某一天或当前小时。For example, you might use DatePart to calculate the day of the week or the current hour.

如果选择DateInterval.Weekday Interval参数,则返回的值FirstDayOfWeek与枚举的值保持一致。If you choose DateInterval.Weekday for the Interval argument, the returned value is consistent with the values of the FirstDayOfWeek enumeration. 如果DateInterval.WeekOfYear选择, DatePart将使用Calendar 命名System.Globalization空间CultureInfo的和类来确定当前设置。If you choose DateInterval.WeekOfYear, DatePart uses the Calendar and CultureInfo classes of the System.Globalization namespace to determine your current settings.

参数影响DateInterval.Weekday使用和DateInterval.WeekOfYear设置的计算。 Interval FirstDayOfWeekValueThe FirstDayOfWeekValue argument affects calculations that use the DateInterval.Weekday and DateInterval.WeekOfYear Interval settings. 参数会影响为指定DateInterval.WeekOfYearInterval计算。 FirstWeekOfYearValueThe FirstWeekOfYearValue argument affects calculations that specify DateInterval.WeekOfYear for Interval.

由于DateTime结构Date支持每个值,因此它的方法为您在检索日期/时间部分中指定了其他选项。Since every Date value is supported by a DateTime structure, its methods give you additional options in retrieving date/time parts. 例如,可以获取Date变量的整个日期值,时间值设置为午夜,如下所示:For example, you can obtain the entire date value of a Date variable, with the time value set to midnight, as follows:

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

Interval参数可以具有下列设置之一。The Interval argument can have one of the following settings.

枚举值Enumeration value StringString 要返回的日期/时间值的一部分Part of date/time value to return
DateInterval.Day dd 一月的某一日(1 到 31)Day of month (1 through 31)
DateInterval.DayOfYear yy 一年的某一日(1 到 366)Day of year (1 through 366)
DateInterval.Hour hh 小时Hour
DateInterval.Minute nn 分钟Minute
DateInterval.Month mm 月份Month
DateInterval.Quarter qq 分之Quarter
DateInterval.Second ss Second
DateInterval.Weekday ww 一周的某一日(1 到 7)Day of week (1 through 7)
DateInterval.WeekOfYear 人员ww 一年的某一周(1 到 53)Week of year (1 through 53)
DateInterval.Year yyyyyyyy Year

FirstDayOfWeekValue参数可以具有下列设置之一。The FirstDayOfWeekValue argument can have one of the following settings.

枚举值Enumeration value Value 说明Description
FirstDayOfWeek.System 00 系统设置中指定的周的第一天First day of week specified in system settings
FirstDayOfWeek.Sunday 11 星期日(默认值)Sunday (default)
FirstDayOfWeek.Monday 22 星期一(符合 ISO 标准8601,第3.17 节)Monday (complies with ISO standard 8601, section 3.17)
FirstDayOfWeek.Tuesday 33 星期二Tuesday
FirstDayOfWeek.Wednesday 44 星期三Wednesday
FirstDayOfWeek.Thursday 55 星期四Thursday
FirstDayOfWeek.Friday 66 星期五Friday
FirstDayOfWeek.Saturday 77 星期六Saturday

FirstWeekOfYearValue参数可以具有下列设置之一。The FirstWeekOfYearValue argument can have one of the following settings.

枚举值Enumeration value Value 说明Description
FirstWeekOfYear.System 00 系统设置中指定的年份的第一周First week of year specified in system settings
FirstWeekOfYear.Jan1 11 1月1日所在的周(默认值)Week in which January 1 occurs (default)
FirstWeekOfYear.FirstFourDays 22 新年份中至少为四天的周(符合 ISO 标准8601,第3.17 节)Week that has at least four days in the new year (complies with ISO standard 8601, section 3.17)
FirstWeekOfYear.FirstFullWeek 33 新年份中的第一个完整周First full week in new year

另请参阅

DatePart(String, Object, FirstDayOfWeek, FirstWeekOfYear)

返回一个值,其中包含给定 Date 值的指定组件。Returns an value containing the specified component of a given Date value.

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

必需。Required. DateInterval 枚举值或字符串表达式,它表示要返回的日期/时间值部分。A DateInterval enumeration value or a string expression representing the part of the date/time value you want to return.

DateValue
Object

必需。Required. 要计算的 Date 值。A Date value that you want to evaluate.

DayOfWeek
FirstDayOfWeek

可选。Optional. FirstDayOfWeek 枚举中选择的值,用于指定一周的第一天。A value chosen from the FirstDayOfWeek enumeration that specifies the first day of the week. 如果未指定,则使用 SundayIf not specified, Sunday is used.

WeekOfYear
FirstWeekOfYear

可选。Optional. FirstWeekOfYear 枚举中选择的值,用于指定一年的第一周。A value chosen from the FirstWeekOfYear enumeration that specifies the first week of the year. 如果未指定,则使用 Jan1If not specified, Jan1 is used.

返回

包含给定 Date 值的指定组件的整数值。An integer value containing the specified component of a given Date value.

异常

Interval 无效。Interval is invalid.

DateValue 并不强制为 DateDateValue is not coercible to Date.

示例

此示例使用了一个日期,并使用DatePart函数来显示该日期在一年中的哪个季度。This example takes a date and, using the DatePart function, displays the quarter of the year in which it occurs.

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函数来评估日期/时间值并返回特定的组件。You can use the DatePart function to evaluate a date/time value and return a specific component. 例如,你可以使用DatePart来计算一周中的某一天或当前小时。For example, you might use DatePart to calculate the day of the week or the current hour.

如果选择DateInterval.Weekday Interval参数,则返回的值FirstDayOfWeek与枚举的值保持一致。If you choose DateInterval.Weekday for the Interval argument, the returned value is consistent with the values of the FirstDayOfWeek enumeration. 如果DateInterval.WeekOfYear选择, DatePart将使用Calendar 命名System.Globalization空间CultureInfo的和类来确定当前设置。If you choose DateInterval.WeekOfYear, DatePart uses the Calendar and CultureInfo classes of the System.Globalization namespace to determine your current settings.

参数影响DateInterval.Weekday使用和DateInterval.WeekOfYear设置的计算。 Interval FirstDayOfWeekValueThe FirstDayOfWeekValue argument affects calculations that use the DateInterval.Weekday and DateInterval.WeekOfYear Interval settings. 参数会影响为指定DateInterval.WeekOfYearInterval计算。 FirstWeekOfYearValueThe FirstWeekOfYearValue argument affects calculations that specify DateInterval.WeekOfYear for Interval.

由于DateTime结构Date支持每个值,因此它的方法为您在检索日期/时间部分中指定了其他选项。Since every Date value is supported by a DateTime structure, its methods give you additional options in retrieving date/time parts. 例如,可以获取Date变量的整个日期值,时间值设置为午夜,如下所示:For example, you can obtain the entire date value of a Date variable, with the time value set to midnight, as follows:

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

Interval参数可以具有下列设置之一。The Interval argument can have one of the following settings.

枚举值Enumeration value StringString 要返回的日期/时间值的一部分Part of date/time value to return
DateInterval.Day dd 一月的某一日(1 到 31)Day of month (1 through 31)
DateInterval.DayOfYear yy 一年的某一日(1 到 366)Day of year (1 through 366)
DateInterval.Hour hh 小时Hour
DateInterval.Minute nn 分钟Minute
DateInterval.Month mm 月份Month
DateInterval.Quarter qq 分之Quarter
DateInterval.Second ss Second
DateInterval.Weekday ww 一周的某一日(1 到 7)Day of week (1 through 7)
DateInterval.WeekOfYear 人员ww 一年的某一周(1 到 53)Week of year (1 through 53)
DateInterval.Year yyyyyyyy Year

FirstDayOfWeekValue参数可以具有下列设置之一。The FirstDayOfWeekValue argument can have one of the following settings.

枚举值Enumeration value Value 说明Description
FirstDayOfWeek.System 00 系统设置中指定的周的第一天First day of week specified in system settings
FirstDayOfWeek.Sunday 11 星期日(默认值)Sunday (default)
FirstDayOfWeek.Monday 22 星期一(符合 ISO 标准8601,第3.17 节)Monday (complies with ISO standard 8601, section 3.17)
FirstDayOfWeek.Tuesday 33 星期二Tuesday
FirstDayOfWeek.Wednesday 44 星期三Wednesday
FirstDayOfWeek.Thursday 55 星期四Thursday
FirstDayOfWeek.Friday 66 星期五Friday
FirstDayOfWeek.Saturday 77 星期六Saturday

FirstWeekOfYearValue参数可以具有下列设置之一。The FirstWeekOfYearValue argument can have one of the following settings.

枚举值Enumeration value Value 说明Description
FirstWeekOfYear.System 00 系统设置中指定的年份的第一周First week of year specified in system settings
FirstWeekOfYear.Jan1 11 1月1日所在的周(默认值)Week in which January 1 occurs (default)
FirstWeekOfYear.FirstFourDays 22 新年份中至少为四天的周(符合 ISO 标准8601,第3.17 节)Week that has at least four days in the new year (complies with ISO standard 8601, section 3.17)
FirstWeekOfYear.FirstFullWeek 33 新年份中的第一个完整周First full week in new year

另请参阅

适用于