DatePart function
Returns the specified portion of a given date.
DatePart(interval, date[, firstdayofweek[, firstweekofyear]])
Arguments
Interval
Required option. A string expression represents the time interval to return. For numeric values, see the "Settings" section.
Date
Required option. The date expression to evaluate.
Firstdayof Week
Options available. Specifies the constant for the first day of the week. If not specified, the default is Sunday. For numeric values, see the "Settings" section.
FirstWeekOfYear
Options available. Specifies a constant for the first week of the year. If not specified, the default is the week of January 1. For numeric values, see the "Settings" section.
Set up
The interval parameter can have the following values:
set |
description |
yyyy |
year |
q |
quarter |
m< /td> |
month |
y |
number of days in a year |
d |
|
w< /td> |
|
ww
week |
h |
hours |
n |
minutes |
s |
sec |
The FirstDayOfWeek parameter can have the following values:
Constants |
value |
Description |
Vbusesystem |
0 |
Use the regional language Support (NLS) API settings. |
Vbsunday |
1 |
Sunday (default) |
Vbmonday |
2 |
Monday |
Vbtuesday |
3 |
Tuesday |
Vbwednesday |
4 |
Wednesday |
Vbthursday |
5 |
Thursday |
Vbfriday |
6 |
Friday |
Vbsaturday |
7 |
Saturday |
The FirstWeekOfYear parameter can have the following values:
Constants |
value |
Description |
Vbusesystem |
0 |
Use the regional language Support (NLS) API settings. |
VbFirstJan1 |
1 |
Starting from the week of January 1 (default). |
vbFirstFourDays |
2 |
Starting from the first week at least four days in the new year. |
Vbfirstfullweek |
3 |
Start with the first full week (not straddling) in the new Year. |
Description
The DatePart function is used to calculate the date and return the specified time interval. For example, use DatePart to calculate the day of the week or the current time.
The firstdayofweek parameter affects the calculation using the "W" and "ww" interval symbols.
If date is a date literal, the specified year becomes a fixed part of the date. However, if date is enclosed in quotation marks ("") and the year is omitted, the current year is inserted each time the date expression is evaluated in the code. This allows you to write program code that works for different years.
The following example uses the DatePart function to obtain a date and displays the season in which the day is located.
Function GetQuarter(TheDate) DatePart("q", TheDate)End Function