DatePart Method (DateInterval, DateTime, FirstDayOfWeek, FirstWeekOfYear)
Collapse the table of content
Expand the table of content
This documentation is archived and is not being maintained.

DateAndTime.DatePart Method (DateInterval, DateTime, FirstDayOfWeek, FirstWeekOfYear)

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

Namespace: Microsoft.VisualBasic
Assembly: Microsoft.VisualBasic (in microsoft.visualbasic.dll)

public static int DatePart (
	DateInterval Interval,
	DateTime DateValue,
	[OptionalAttribute] FirstDayOfWeek FirstDayOfWeekValue,
	[OptionalAttribute] FirstWeekOfYear FirstWeekOfYearValue
)
public static int DatePart (
	DateInterval Interval, 
	DateTime DateValue, 
	/** @attribute OptionalAttribute() */ FirstDayOfWeek FirstDayOfWeekValue, 
	/** @attribute OptionalAttribute() */ FirstWeekOfYear FirstWeekOfYearValue
)
public static function DatePart (
	Interval : DateInterval, 
	DateValue : DateTime, 
	FirstDayOfWeekValue : FirstDayOfWeek, 
	FirstWeekOfYearValue : FirstWeekOfYear
) : int

Parameters

Interval

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

DateValue

Required. Date value that you want to evaluate.

FirstDayOfWeekValue

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

FirstWeekOfYearValue

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

Return Value

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

For more detailed information, see the Visual Basic topic DatePart Function (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.

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

This example takes a date and, using the DatePart function, displays the quarter of the year in which it occurs.

Dim FirstDate, Msg As String   'Declare variables.
Dim SecondDate As Date
FirstDate = InputBox("Enter a date:")
SecondDate = CDate(FirstDate)
Msg = "Quarter: " & DatePart(DateInterval.Quarter, SecondDate)
MsgBox(Msg)

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0
Show:
© 2016 Microsoft