DatePart
Returns a Variant (Integer) containing the specified part of a given date.
Syntax: DatePart ( interval, date [, firstdayofweek [, firstweekofyear ]] )
- interval
- required String expression that is the interval of time to return. See Interval settings.
- date
- required Variant (Date) value to evaluate.
- firstdayofweek
- optional A VbDayOfWeek constant specifying the first day of the week. Defaults to vbSunday.
- firstweekofyear
- optional A VbFirstWeekOfYear constant specifying the first week of the year. Defaults to vbFirstJan1.
Interval 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 |
firstdayofweek settings
| Constant | Value | Description |
|---|---|---|
| vbUseSystem | 0 | 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 settings
| Constant | Value | Description |
|---|---|---|
| vbUseSystem | 0 | NLS API setting. |
| vbFirstJan1 | 1 | Week in which January 1 occurs (default). |
| vbFirstFourDays | 2 | First week that has at least four days in the new year. |
| vbFirstFullWeek | 3 | First full week of the year. |
The firstdayofweek argument affects calculations that use the “w” and “ww” interval symbols.
Warning BETA 1034
With the “w” or “ww” interval, DatePart raises no error for a firstdayofweek outside 0 to 7, and what it returns follows no rule: a negative value acts as if taken modulo 7, 8 acts as vbSunday, and a larger value matches no setting. DatePart("w", #1/1/2000#, 255) returns -2. VB6 raises error 5.
Warning BETA 1034
With the “ww” interval, DatePart goes wrong for a date in the first days of year 100 whose week belongs to year 99. With vbFirstFullWeek it never returns, and the program stops responding: DatePart("ww", #1/1/100#, vbSunday, vbFirstFullWeek) is one such call. With vbFirstFourDays it returns a wrong number: -98980 for January 1, 100. VB6 returns 52 for both.
If date is a date literal, the specified year becomes a permanent part of that date. If date is enclosed in double quotation marks and the year is omitted, the current year is inserted each time the expression is evaluated.
If the Calendar property setting is Gregorian, the supplied date must be Gregorian. If the calendar is Hijri, the supplied date must be Hijri. The returned date part is in the time period units of the current calendar.
Common Errors
| Error | When |
|---|---|
5 (&H5), Invalid procedure call or argument | interval is not one of the settings listed and date is not Null interval is not one of the settings listed and date is Null |
13 (&HD), Type mismatch | date is a string that is not a date, or a number outside the range of dates |
94 (&H5E), Invalid use of Null | interval, firstdayofweek or firstweekofyear is Null |
Note BETA 1034
When interval is not one of the settings listed and date is Null, VB6 and 64-bit VBA raise no error.
Note BETA 1034
When interval is “w” or “ww” and firstdayofweek is outside 0 to 7, VB6 and 64-bit VBA raise error 5 (&H5), Invalid procedure call or argument; twinBASIC raises none.
Note BETA 1034
When interval is “ww” and firstweekofyear is outside 0 to 3, VB6 and 64-bit VBA raise error 5 (&H5), Invalid procedure call or argument; twinBASIC raises none.
Example
This example takes a date and, using the DatePart function, displays the quarter of the year in which it occurs.
Dim TheDate As Date
TheDate = InputBox("Enter a date:")
MsgBox "Quarter: " & DatePart("q", TheDate)