The syntax of DatePart is DatePart (interval, date), which is used to obtain a part of date.
Interval
Yyyy: Year in date
M: Month in date
D: Day in date
H: hour in date
N: minute in date
S: seconds in date
Q: Which quarter of the year is date, for example, 2nd.
Y: date is the day of the year, for example, is the day 180th.
W: date is the day of the week (Sunday is the first day by default). For example, 2nd is the day of the week.
Ww: the week in which date is located, for example, is in week 27th.
DatePart (interval, date [, firstdayofweek [, firstweekofyear])
The syntax of the DatePart function includes the following parameters:
Parameter description
Interval is required. String expression, indicating the time interval to return. For values, see the "Settings" section.
Date is required. The date expression to be calculated.
Firstdayof week is optional. Specifies the constant of the first day of a week. If not specified, the default value is Sunday. For values, see the "Settings" section.
Firstweekofyear is optional. Specifies the constant of the first week of a year. If this parameter is not specified, the default value is the week of April 1, January 1. For values, see the "Settings" section.
You can set the interval parameter to the following values:
Set description
Yyyy
Q quarter
M month
Y number of days in a year
D
Number of days per week
Ww week
H hour
M minutes
S seconds
The firstdayofweek parameter can have the following values:
Constant Value description
VbUseSystem 0 is set using the regional language (NLS) API.
VbSunday 1 Sunday (default)
VbMonday 2 Monday
VbTuesday 3 Tuesday
VbWednesday 4
VbThursday 5 Thursday
VbFriday 6 Friday
VbSaturday 7 Saturday
The firstweekofyear parameter can have the following values:
Constant Value description
VbUseSystem 0 is set using the regional language (NLS) API.
VbFirstJan1 starts from the week of July 1, January 1 (default ).
VbFirstFourDays 2 starts from the first week of at least four days in the New Year.
VbFirstFullWeek 3 starts from the first full week (not cross year) in the new year.
Description
The DatePart function is used to calculate the date and return the specified time interval. For example, DatePart is used to calculate the day of a week or the current time.
The firstdayofweek parameter affects the calculation of the interval between "w" and "ww.
If date is a date text, the specified year will become a fixed part of the date. However, if date is included in quotation marks ("") and the year is omitted, the current year will be inserted every time the date expression is calculated in the code. In this way, you can write program code for different years.