DatePart Function

Returns the specified part of a given date.

Syntax

DatePart(interval, date)

Arguments:

  • Interval: Required. String expression that is the interval of time you want to return. See Settings section for values.
  • Date: Required. Date expression you want to evaluate.
Settings: The interval argument can have the following values:

Table 11-11 Interval Argument Values

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

Supported Date Formats:

  • MM-DD-YYYY
  • MM/DD/YYYY
  • DD-MM-YYYY
  • DD/MM/YYYY
  • YYYY-MM-DD
  • YYYY/MM/DD

Supported Time Format:

hh:mm:ss

Remarks

You can use the DatePart function to evaluate a date and return a specific interval of time. For example, you might use DatePart to calculate the day of the week or the current hour.

These examples take a date and, using the DatePart function, displays the quarter of the year in which it occurs.

Example 1:

Function GetQuarter(TheDate)
GetQuarter = DatePart("q", TheDate)
End Function

Example 2:

Dim MyDate, YearPart
MyDate = "21/01/2025"
YearPart = DatePart("yyyy", MyDate)
'Output: 2025

Example 3:

Dim MyDate, MonthPart
MyDate = "21/01/2025"
MonthPart = DatePart("m", MyDate) 
'Output: MonthPart contains 1 (January)

Example 4:

Dim MyDate, DayPart
MyDate = "21/01/2025"
DayPart = DatePart("d", MyDate) 
'Output: 21

Example 5:

Dim MyDate, HourPart
MyDate = "21/01/2025 14:30:00" ' Includes time component.
HourPart = DatePart("h", MyDate) 
'Output: 14 (2 PM)

Example 6:

Dim MyDate, MinutePart
MyDate = "21/01/2025 14:30:00" 
MinutePart = DatePart("n", MyDate) 
'Output: 30

Example 7:

Dim MyDate, SecondPart
MyDate = "21/01/2025 14:30:00" ' Includes time component.
SecondPart = DatePart("s", MyDate) ' Returns the second part of the date.
'Output: 0