VBA DatePart Function

Written by

Editorial Team

Reviewed by

Steve Rynearson

Last updated on July 21, 2021

DatePart Description

Returns a specified part of a given date.


Simple DatePart Examples

Here is a simple DatePart example:

Sub DatePart_Year_Test()
    MsgBox DatePart("yyyy", #1/1/2019#)
End Sub

This code will return 2019.


DatePart Syntax

In the VBA Editor, you can type  “DatePart(” to see the syntax for the DatePart Function:

The DatePart function contains 4 arguments:

Interval: Time unit (Days, Months, Years, etc.). Enter as string. (ex. “m” for Month)

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

Date: Varient (Date) value that you want to evaluate.

FirstDayOfWeek: A constant that specifies the first day of the week. This is optional. If not specified, Sunday is assumed.

Constant Value Description
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: A constant that specifies the first week of the year. This is optional. If not specified, the first week is assumed to be the week in which January 1 occurs.

Constant Value Description
vbUseSystem 0 Use the NLS API setting.
vbFirstJan1 1 Start with week in which January 1 occurs (default).
vbFirstFourDays 2 Start with the first week that has at least four days in the new year.
vbFirstFullWeek 3 Start with first full week of the year.


Examples of Excel VBA DatePart Function

Referencing Dates

To start, we will demonstrate different ways to reference dates using the VBA DatePart Function.

Each of these DatePart functions produce the same result:

Sub DateAdd_ReferenceDates()

    MsgBox DatePart("yyyy", #4/1/2019#)

    MsgBox DatePart("yyyy", DateSerial(2019, 4, 1))

    MsgBox DatePart("yyyy", DateValue("April 1, 2019"))

End Sub

Or you can reference a cell containing a date:

Sub DatePart_ReferenceDate_Cell()

    MsgBox DatePart("yyyy", Range("C2").Value)
End Sub

Or create and reference date variables:

Sub DatePart_Variable()

    Dim dt As Date
    dt = #4/1/2019#

    MsgBox DateAdd("yyyy", dt)

End Sub


Using Different Units of Interval


Sub DatePart_Quarter()
    MsgBox DatePart("q", #6/30/2019#)
End Sub


Sub DatePart_Month()
    MsgBox DatePart("m", #6/30/2019#)
    ' equivalent
    MsgBox Month(#6/30/2019#)
End Sub


Sub DatePart_Day()
    MsgBox DatePart("d", #6/30/2019#)
    ' equivalent
    MsgBox Day(#6/30/2019#)
End Sub


Sub DatePart_Week_Test()
    MsgBox DatePart("w", #6/30/2019#)
    ' equivalent
    MsgBox Weekday(#6/30/2019#)
End Sub


Sub DatePart_Hour()
    Dim dt As Date
    Dim nHour As Long
    dt = #8/14/2019 9:30:00 AM#
    nHour = DatePart("h", dt)

    MsgBox nHour
    ' equivalent
    MsgBox Hour(dt)
End Sub


Sub DatePart_Minute()
    MsgBox DatePart("n", #8/14/2019 9:15:00 AM#)
    ' equivalent
    MsgBox Minute(#8/14/2019 9:15:00 AM#)
    MsgBox Minute(#9:15:00 AM#)
End Sub


Sub DatePart_Second()
    MsgBox DatePart("s", #8/14/2019 9:15:15 AM#)
    ' equivalent
    MsgBox Second(#8/14/2019 9:15:15 AM#)
    MsgBox Second(#9:15:15 AM#)
End Sub



VBA Coding Made Easy

Stop searching for VBA code online. Learn more about AutoMacro - A VBA Code Builder that allows beginners to code procedures from scratch with minimal coding knowledge and with many time-saving features for all users! vba save as

Learn More!

VBA Code Examples Add-in

Easily access all of the code examples found on our site.

Simply navigate to the menu, click, and the code will be inserted directly into your module. .xlam add-in.

(No installation required!)

Free Download

Return to VBA Code Examples