แก้ไข

แชร์ผ่าน


DateAndTime.DatePart Method

Definition

Returns an integer value containing the specified component of a given Date value.

Overloads

DatePart(DateInterval, DateTime, FirstDayOfWeek, FirstWeekOfYear)

Returns an integer value containing the specified component of a given Date value.

DatePart(String, Object, FirstDayOfWeek, FirstWeekOfYear)

Returns an value containing the specified component of a given Date value.

DatePart(DateInterval, DateTime, FirstDayOfWeek, FirstWeekOfYear)

Source:
DateAndTime.vb
Source:
DateAndTime.vb
Source:
DateAndTime.vb

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

Parameters

Interval
DateInterval

Required. A DateInterval enumeration value or a string expression representing the part of the date/time value you want to return.

DateValue
DateTime

Required. The Date value that you want to evaluate.

FirstDayOfWeekValue
FirstDayOfWeek

Optional. A value chosen from the FirstDayOfWeek enumeration that specifies the first day of the week. If not specified, Sunday is used.

FirstWeekOfYearValue
FirstWeekOfYear

Optional. A value chosen from the FirstWeekOfYear enumeration that specifies the first week of the year. If not specified, Jan1 is used.

Returns

An integer value containing the specified component of a given Date value.

Exceptions

Interval is not valid.

DateValue is not coercible to Date.

Examples

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)

Remarks

You can use the DatePart function to evaluate a date/time value and return a specific component. For example, you might use DatePart to calculate the day of the week or the current hour.

If you choose DateInterval.Weekday for the Interval argument, the returned value is consistent with the values of the FirstDayOfWeek enumeration. If you choose DateInterval.WeekOfYear, DatePart uses the Calendar and CultureInfo classes of the System.Globalization namespace to determine your current settings.

The FirstDayOfWeekValue argument affects calculations that use the DateInterval.Weekday and DateInterval.WeekOfYear Interval settings. The FirstWeekOfYearValue argument affects calculations that specify DateInterval.WeekOfYear for Interval.

Since every Date value is supported by a DateTime structure, its methods give you additional options in retrieving date/time parts. 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.  

The Interval argument can have one of the following settings.

Enumeration value String Part of date/time value to return
DateInterval.Day d Day of month (1 through 31)
DateInterval.DayOfYear y Day of year (1 through 366)
DateInterval.Hour h Hour
DateInterval.Minute n Minute
DateInterval.Month m Month
DateInterval.Quarter q Quarter
DateInterval.Second s Second
DateInterval.Weekday w Day of week (1 through 7)
DateInterval.WeekOfYear ww Week of year (1 through 53)
DateInterval.Year yyyy Year

The FirstDayOfWeekValue 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

The FirstWeekOfYearValue argument can have one of the following settings.

Enumeration value Value Description
FirstWeekOfYear.System 0 First week of year specified in system settings
FirstWeekOfYear.Jan1 1 Week in which January 1 occurs (default)
FirstWeekOfYear.FirstFourDays 2 Week that has at least four days in the new year (complies with ISO standard 8601, section 3.17)
FirstWeekOfYear.FirstFullWeek 3 First full week in new year

See also

Applies to

DatePart(String, Object, FirstDayOfWeek, FirstWeekOfYear)

Source:
DateAndTime.vb
Source:
DateAndTime.vb
Source:
DateAndTime.vb

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);
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

Parameters

Interval
String

Required. A DateInterval enumeration value or a string expression representing the part of the date/time value you want to return.

DateValue
Object

Required. A Date value that you want to evaluate.

DayOfWeek
FirstDayOfWeek

Optional. A value chosen from the FirstDayOfWeek enumeration that specifies the first day of the week. If not specified, Sunday is used.

WeekOfYear
FirstWeekOfYear

Optional. A value chosen from the FirstWeekOfYear enumeration that specifies the first week of the year. If not specified, Jan1 is used.

Returns

An integer value containing the specified component of a given Date value.

Exceptions

Interval is invalid.

DateValue is not coercible to Date.

Examples

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)

Remarks

You can use the DatePart function to evaluate a date/time value and return a specific component. For example, you might use DatePart to calculate the day of the week or the current hour.

If you choose DateInterval.Weekday for the Interval argument, the returned value is consistent with the values of the FirstDayOfWeek enumeration. If you choose DateInterval.WeekOfYear, DatePart uses the Calendar and CultureInfo classes of the System.Globalization namespace to determine your current settings.

The FirstDayOfWeekValue argument affects calculations that use the DateInterval.Weekday and DateInterval.WeekOfYear Interval settings. The FirstWeekOfYearValue argument affects calculations that specify DateInterval.WeekOfYear for Interval.

Since every Date value is supported by a DateTime structure, its methods give you additional options in retrieving date/time parts. 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.  

The Interval argument can have one of the following settings.

Enumeration value String Part of date/time value to return
DateInterval.Day d Day of month (1 through 31)
DateInterval.DayOfYear y Day of year (1 through 366)
DateInterval.Hour h Hour
DateInterval.Minute n Minute
DateInterval.Month m Month
DateInterval.Quarter q Quarter
DateInterval.Second s Second
DateInterval.Weekday w Day of week (1 through 7)
DateInterval.WeekOfYear ww Week of year (1 through 53)
DateInterval.Year yyyy Year

The FirstDayOfWeekValue 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

The FirstWeekOfYearValue argument can have one of the following settings.

Enumeration value Value Description
FirstWeekOfYear.System 0 First week of year specified in system settings
FirstWeekOfYear.Jan1 1 Week in which January 1 occurs (default)
FirstWeekOfYear.FirstFourDays 2 Week that has at least four days in the new year (complies with ISO standard 8601, section 3.17)
FirstWeekOfYear.FirstFullWeek 3 First full week in new year

See also

Applies to