Share this page:

MS Excel: DATEPART Function (VBA)

This Excel tutorial explains how to use the Excel DATEPART function with syntax and examples.

Description

The Microsoft Excel DATEPART function returns a specified part of a given date.

Syntax

The syntax for the Microsoft Excel DATEPART function is:

DatePart( interval, date, [firstdayofweek], [firstweekofyear] )

Parameters or Arguments

interval is the interval of time that you wish to return. This parameter can be any one of the following valid interval values:

Interval Explanation
yyyy Year
q Quarter
m Month
y Day of year
d Day
w Weekday
ww Week
h Hour
n Minute
s Second

date is the date value that you wish to evaluate.

firstdayofweek is optional. It is a constant that specifies the first day of the week. If this parameter is omitted, Excel assumes that Sunday is the first day of the week. This parameter can be one of the following values:

Constant Value Explanation
vbUseSystem 0 Use the NLS API setting
vbSunday 1 Sunday (default)
vbMonday 2 Monday
vbTuesday 3 Tuesday
vbWednesday 4 Wednesday
vbThursday 5 Thursday
vbFriday 6 Friday
vbSaturday 7 Saturday

firstweekofyear is optional. It is a constant that specifies the first week of the year. If this parameter is omitted, Excel assumes that the week containing Jan 1st is the first week of the year. This parameter can be one of the following values:

Constant Value Explanation
vbUseSystem 0 Use the NSL API setting
vbFirstJan1 1 Use the first week that includes Jan 1st (default)
vbFirstFourDays 2 Use the first week in the year that has at least 4 days
vbFirstFullWeek 3 Use the first full week of the year

Applies To

The DATEPART function can be used in the following versions of Microsoft Excel:

  • Excel 2013, Excel 2011 for Mac, Excel 2010, Excel 2007, Excel 2003, Excel XP, Excel 2000

Type of Excel Function

The DATEPART function can be used in Microsoft Excel as the following type of function:

  • VBA function (VBA)

Example (as VBA Function)

The DATEPART function can only be used in VBA code in Microsoft Excel.

Let's look at some Excel DATEPART function examples and explore how to use the DATEPART function in Excel VBA code:

DatePart("yyyy", "15/10/2012")
Result: 2012

DatePart("m", "15/10/2012")
Result: 10

DatePart("d", "15/10/2012")
Result: 15

For example:

Dim LValue As Integer

LValue = DatePart("d", "15/10/2012")

In this example, the variable called LValue would now contain the value of 15.