Description
DatePart returns a Number that specifies a given part of a given date.
Overloads
DatePart (intervalType, inputDateTime)
DatePart (intervalType, inputDateTime, firstDayOfWeek)
DatePart (intervalType, inputDateTime, firstDayOfWeek, firstWeekOfYear)
Arguments
intervalType a String expression that specifies the part of a date to be returned.
inputDateTime is the DateTime value whose part will be extracted.
firstDayOfWeek is an optional constant used to specify the first day of the week. If not specified, crSunday is assumed. For more information, see
Day of Week constants.
firstWeekOfYear is an optional constant specifying the first week of the year. If not specified, the first week is assumed to be the one in which Jan. 1 occurs (crFirstJan1). For more information, see
First Week of Year constants.
Argument values
IntervalType values can be one of the following:
| |
|---|
| |
| Quarter (the result is 1, 2, 3 or 4) |
| Month (the result is from 1 to 12) |
| Day of year (1 to 365 or 366 in a leap year) |
| Day part of the date (1 to 31) |
| Day of week (1 to 7 with the result depending on firstDayOfWeek) |
| Week of year (1 to 53 with firstDayOfWeek and firstWeekOfYear determining the exact days of the first calendar week of the year) |
| Extracts the hour part of the given DateTime (0 to 23) |
| |
| |
Action
DatePart returns a Number that specifies a given part of a given date.
Examples
DatePart ("d", #August 15, 1999#)Returns 15.
DatePart ("m", #August 15, 1999#)Returns 8.
DatePart ("n", #10:35 am#)Returns 35.
DatePart ("q", #September 29, 1999#)Returns 3 since September 29 is in the third quarter of the year.
DatePart ("ww", #September 14, 1997#)Returns 38 since September 14, 1997 is in the 38th week of 1997.
Suppose that a company wants to stamp an id number on each of its shipments for quality control purposes. The id is composed of the order id, followed by a dash, followed by the week the order was shipped, followed by the last two digits of the year. Here is an example formula that produces these ids:
CStr({Orders.Order ID}, 0, "") & "-" &
CStr(DatePart("ww", {Orders.Ship Date}), 0) &
CStr({Orders.Ship Date}, "yy")
Returns the string "2082-3897" if {Orders.Order ID} is 2082 and {Orders.Ship Date} is September 14, 1997.
Comments
The DatePart function with "yyyy" intervalType argument is the same as the Year function. Similarly, the DatePart function with "m", "d", "w", "h", "n" and "s" intervalType argument is the same as the functions Month, Day, Weekday (or DayOfWeek), Hour, Minute and Second respectively. On the other hand, there is no easy alternative to using the DatePart function for the "q", "y" and "ww" intervalType arguments.
The firstDayOfWeek argument affects the DatePart function when the interval type argument is "w" or "ww". For all other intervalType argument values, it is ignored.
The firstWeekOfYear argument affects the DatePart function only when the intervalType argument is "ww". For all other intervalType argument values, it is ignored.