DatePart has been explained in the ASP (VBScript) reference manual, but its explanation of the first parameter is too written and misleading. Now let’s re-explain the syntax of DatePart as DatePart(interval, date), which is used to Get some 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: In which quarter of the year the date is located, for example, 2009-6-29 is in the 2nd quarter.
y: date is on the day of the year, for example, 2009-6-29 is on the 180th day.
w: date is on the day of the week (Sunday is the first day by default), for example, 2009-6-29 is on the 2nd day.
ww: date is located in the week of the year, for example, 2009-6-29 is located in the 27th week.
DatePart(interval, date[, firstdayofweek[, firstweekofyear]])
The syntax of the DatePart function has the following parameters:
Parameter description
interval is required. A string expression representing the time interval to return. See the Settings section for numerical values.
date is required. The date expression to evaluate.
firstdayof week optional. A constant that specifies the first day of the week. If not specified, it defaults to Sunday. See the Settings section for numerical values.
firstweekofyear Optional. Constant that specifies the first week of the year. If not specified, it defaults to the week of January 1st. See the Settings section for numerical values.
The interval parameter can be set to the following values:
Setting description
yyyy year
q quarter
m month
y the number of days in a year
d day
w Day of the week
ww weeks
h hours
m minutes
s seconds
The firstdayofweek parameter can have the following values:
Constant value description
vbUseSystem 0 Use the National Language Support (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 parameter can have the following values:
Constant value description
vbUseSystem 0 Use the National Language Support (NLS) API setting.
vbFirstJan1 1 Starts with the week of January 1st (default).
vbFirstFourDays 2 starts with the first week of the new year that has at least four days.
vbFirstFullWeek 3 starts with the first full week of the new year (not across years).
illustrate
The DatePart function calculates a date and returns a specified time interval. For example, use DatePart to calculate the day of the week or the current time on a certain day.
The firstdayofweek parameter affects calculations using w and ww interval symbols.
If date is a date literal, the specified year becomes a fixed part of the date. But if the date is enclosed in quotation marks ( ) and the year is omitted, the current year will be inserted every time the date expression is evaluated in the code. This makes it possible to write program code that works for different years.