Return to VBA Code Examples

VBA DatePart Function

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

Quarter

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

Month

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

Day

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

Week

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

Hour

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

Minutes

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

Second

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!