DatePart Function (Visual Basic)
Updated: August 2008
Returns an Integer value containing the specified component of a given Date value.
Public Overloads Function DatePart( _ ByVal Interval As DateInterval, _ ByVal DateValue As DateTime, _ Optional ByVal FirstDayOfWeekValue As FirstDayOfWeek = VbSunday, _ Optional ByVal FirstWeekOfYearValue As FirstWeekOfYear = VbFirstJan1 _ ) As Integer ' -or- Public Overloads Function DatePart( _ ByVal Interval As String, _ ByVal DateValue As Object, _ Optional ByVal DayOfWeek As FirstDayOfWeek = FirstDayOfWeek.Sunday, _ Optional ByVal WeekOfYear As FirstWeekOfYear = FirstWeekOfYear.Jan1 _ ) As Integer
The Interval argument can have one of the following settings.
Part of date/time value to return
Day of month (1 through 31)
Day of year (1 through 366)
Day of week (1 through 7)
Week of year (1 through 53)
The FirstDayOfWeekValue argument can have one of the following settings.
First day of week specified in system settings
Monday (complies with ISO standard 8601, section 3.17)
The FirstWeekOfYearValue argument can have one of the following settings.
First week of year specified in system settings
Week in which January 1 occurs (default)
Week that has at least four days in the new year (complies with ISO standard 8601, section 3.17)
First full week in new year
Interval is invalid.
DateValue is not coercible to Date.
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.
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.
This example takes a date and, using the DatePart function, displays the quarter of the year, day of the month, and week of the year in which it occurs, and the day of the week on which it falls. The comments give the expected results for February 12, 2008.
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)
Assembly: Visual Basic Runtime Library (in Microsoft.VisualBasic.dll)