Returns a specified part of a given date.
DatePart(interval, date[, firstdayofweek[, firstweekofyear]])
The DatePart function syntax has these named arguments:
Part | Description |
interval | String expression, as described in Settings, that is the interval of time you want to return. |
date | Date that you want to evaluate. |
firstdayofweek | A constant, as described in Settings, that specifies the first day of the week. If not specified, Sunday is assumed. |
firstweekofyear | A constant, as described in Settings, that specifies the first week of the year. If not specified, the first week is assumed to be the week in which January 1 occurs. |
The interval argument has these settings:
Setting | Description |
yyyy | Year |
q | Quarter |
m | Month |
y | Day of year |
d | Day |
w | Weekday |
ww | Week |
h | Hour |
n | Minute |
s | Second |
The firstdayofweek argument has these settings:
Constant | Value | Description |
vbUseSystem | 0 | Use application setting if one exists; otherwise use 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 |
The firstweekofyear argument has these settings:
Constant | Value | Description |
vbUseSystem | 0 | Use application setting if one exists; otherwise use 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. |
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.
The firstdayofweek argument affects calculations that use the w and ww interval symbols.
If date is a date literal (a date enclosed by number signs (#)), the year, if specified, becomes a permanent part of that date. However, if date is enclosed in double quotation marks (""), and you omit the year, the current year is inserted in your code each time the date expression is evaluated. This makes it possible to write code that can be used in different years.
DateAdd Function, DateDiff Function, Day Function, Format Function, Now Function, Weekday Function, Year Function.
This example takes a date and, using the DatePart function, displays which quarter of the year it falls in.
Dim TheDate As Date ' Declare variables.Msg= InputBox("Enter a date:")= "Quarter: " & DatePart("q", TheDate)Msg
The following example uses the DatePart function to determine the day of the week on which an order was placed.
Function DayOfWeek (OrderDate As Date) As Integer DayOfWeek = DatePart("w", OrderDate)Function
The next example uses the DatePart function to specify criteria for a select query. For example, suppose you want to create a query based on an Orders table to list all orders placed in the first quarter of 1995. Assuming your Orders table has a OrderID field and an OrderDate field, you can drag the OrderID field to the first cell in the query design grid, and enter the following in the Criteria cell beneath it.
(DatePart("q", [OrderDate]) = 1) and (DatePart("yyyy", [OrderDate]) = 1995)