Use DatePart() to extract the year, month, day, quarter or other part of a date in Classic ASP. This example prints the current month directly:
<%
Option Explicit
Dim d_today
d_today = Date()
Response.Write DatePart("m", d_today)
%>
The result is a number from 1 to 12 for the current month. Use Date() for today’s date or Now() when you also need the current time.
The function returns an integer containing the requested portion of a date value.
DatePart(interval, date[, firstdayofweek[, firstweekofyear]])
| Parameter | Required? | Purpose |
|---|---|---|
interval | Yes | A string such as "yyyy" (year), "m" (month) or "ww" (week number). |
date | Yes | A VBScript date value, such as Date(), Now() or DateSerial(2026, 11, 12). |
firstdayofweek | No | Controls the first weekday when using "w" or "ww"; Sunday is the default. |
firstweekofyear | No | Controls which week is numbered week 1 for "ww"; defaults to the week containing 1 January. |
Important: The interval is a string argument. Use "m" for month and "n" for minute; they are not interchangeable.
All ten interval codes supported by VBScript are listed below.
| Interval | Extracts | Example |
|---|---|---|
yyyy | Year | DatePart("yyyy", d) |
q | Quarter (1–4) | DatePart("q", d) |
m | Month (1–12) | DatePart("m", d) |
y | Day of year (1–365, or 366) | DatePart("y", d) |
d | Day of month (1–31) | DatePart("d", d) |
w | Day of week (1–7) | DatePart("w", d) |
ww | Week of year | DatePart("ww", d) |
h | Hour (0–23) | DatePart("h", d) |
n | Minute (0–59) | DatePart("n", d) |
s | Second (0–59) | DatePart("s", d) |
"d" returns the day within the month; "y" returns the day within the year. "w" means weekday; "ww" means week number.DateSerial() constructs a date without depending on a country-specific text format. The example uses 12 November 2026.
<%
Option Explicit
Dim myDate
myDate = DateSerial(2026, 11, 12)
Response.Write "Year: " & DatePart("yyyy", myDate) & "<br>"
Response.Write "Month: " & DatePart("m", myDate) & "<br>"
Response.Write "Day: " & DatePart("d", myDate)
%>
Output:
Year: 2026
Month: 11
Day: 12
The original example for #9/10/2003# also returns 2003 with DatePart("yyyy", ...). Use a date value with an explicit year to keep the output repeatable.
Response.Write DatePart("yyyy", #9/10/2003#)
Quarter values are useful for grouping transactions or reports. Day-of-year values are useful when counting calendar days in a leap year.
<%
Option Explicit
Dim myDate
myDate = DateSerial(2024, 2, 29)
Response.Write "Quarter: " & DatePart("q", myDate) & "<br>"
Response.Write "Day of year: " & DatePart("y", myDate) & "<br>"
Response.Write "Day of month: " & DatePart("d", myDate)
%>
Output: Quarter: 1; Day of year: 60; Day of month: 29. The date is in a leap year, so February 29 is the 60th day.
DatePart() also handles the time component of a VBScript date/time value. This example builds a predictable value using DateSerial() and TimeSerial().
<%
Option Explicit
Dim myDateTime
myDateTime = DateSerial(2026, 11, 12) + TimeSerial(14, 35, 9)
Response.Write "Hour: " & DatePart("h", myDateTime) & "<br>"
Response.Write "Minute: " & DatePart("n", myDateTime) & "<br>"
Response.Write "Second: " & DatePart("s", myDateTime)
%>
Output: Hour: 14; Minute: 35; Second: 9.
For live server time, replace the fixed value with Now(). The result then depends on the time zone and clock of the ASP server.
Response.Write DatePart("h", Now())
Build a compact report label from the current date. MonthName() can display the full month name instead of its number.
<%
Option Explicit
Dim todayDate, yearNumber, monthNumber
todayDate = Date()
yearNumber = DatePart("yyyy", todayDate)
monthNumber = DatePart("m", todayDate)
Response.Write "Report period: " & MonthName(monthNumber) & " " & yearNumber
%>
Example output: Report period: November 2026 when the server date is in November 2026. The actual output changes with the server date.
By default, DatePart("w", date) treats Sunday as day 1. Pass vbMonday when you want Monday to be day 1, Tuesday day 2 and so on. For additional weekday formatting, see Weekday() and WeekdayName().
<%
Option Explicit
Dim myDate
myDate = DateSerial(2026, 11, 12)
Response.Write "Sunday-first: " & DatePart("w", myDate) & "<br>"
Response.Write "Monday-first: " & DatePart("w", myDate, vbMonday)
%>
Output: Sunday-first: 5; Monday-first: 4. The date is a Thursday.
For week-of-year calculations, supply both optional arguments when the calendar convention matters:
<%
Option Explicit
Dim myDate, weekNumber
myDate = DateSerial(2026, 11, 12)
weekNumber = DatePart("ww", myDate, vbMonday, vbFirstFourDays)
Response.Write "Week: " & weekNumber
%>
| Setting | Meaning |
|---|---|
vbSunday (1) | Sunday starts the week (default). |
vbMonday (2) | Monday starts the week. |
vbFirstJan1 (1) | Week 1 contains 1 January (default). |
vbFirstFourDays (2) | Week 1 has at least four days in the new year. |
vbFirstFullWeek (3) | Week 1 is the first full seven-day week of the new year. |
DatePart("ww", ...) on the last Monday of some calendar years. Even with vbMonday and vbFirstFourDays, test dates around the New Year before using the result as an authoritative ISO week number.For the three most common date components, VBScript offers shorter built-in functions. Both approaches return the same numeric values for a given date.
<%
Option Explicit
Dim myDate
myDate = DateSerial(2003, 10, 15)
Response.Write "Year(): " & Year(myDate) & "<br>"
Response.Write "Month(): " & Month(myDate) & "<br>"
Response.Write "Day(): " & Day(myDate) & "<br>"
Response.Write "DatePart month: " & DatePart("m", myDate)
%>
Output: Year(): 2003; Month(): 10; Day(): 15; DatePart month: 10.
Use Year(), Month() and Day() when you only need those individual values. Use DatePart() when you also need quarters, week numbers, day-of-year, or time components.
When a date comes from a form or query string, check it with IsDate() before calling DatePart(). CDate() converts a recognized date string to a VBScript date value.
<%
Option Explicit
Dim rawDate, enteredDate
rawDate = Trim(Request.QueryString("date"))
If Len(rawDate) = 0 Then
Response.Write "Add a date parameter to the URL."
ElseIf IsDate(rawDate) Then
enteredDate = CDate(rawDate)
Response.Write "Year: " & DatePart("yyyy", enteredDate) & "<br>"
Response.Write "Month: " & DatePart("m", enteredDate)
Else
Response.Write "Please enter a valid date."
End If
%>
Input format matters: IsDate() and CDate() depend on the server locale. A value such as 03/04/2026 can mean different dates in different regions. If you control separate year, month and day fields, consider DateSerial() instead. For other input patterns, see date input examples.
When displaying user-provided text, encode it for HTML. The example above prints only fixed messages and integer date parts, not raw request data.
DatePart("m", ...) and DatePart("yyyy", ...).DatePart("q", ...) to identify a calendar quarter."d" and "y" when you need day-of-month and day-of-year respectively.firstdayofweek explicitly when weekdays must follow a chosen convention.DatePart() extracts a component; it does not add dates or measure elapsed time. Use DateAdd() for adding intervals and DateDiff() for measuring intervals. For readable output formats, see date formatting in Classic ASP.
VBScript DatePart() and SQL Server DATEPART() are separate functions. The latter runs in a database query and has SQL Server-specific syntax and settings.
| Problem | Cause | Solution |
|---|---|---|
| Type mismatch | The date argument is not recognized as a date. | Use IsDate() before CDate(), or create a date using DateSerial(). |
| Minute returns a month-like value | Used "m" rather than "n". | Use "n" for minutes. |
| Unexpected weekday number | Sunday-first default differs from the expected Monday-first convention. | Pass vbMonday as the third argument. |
| Unexpected week number | Week-start/year-start settings differ, or a year-boundary edge case occurs. | Specify both week settings and test dates near New Year. |
| Unexpected date from numeric text | Locale-dependent interpretation of a date string. | Build a known date with DateSerial(year, month, day). |
| Time components return zero | The value contains a date but no time, such as Date(). | Use Now() or a date/time value containing a time component. |
"d" and "y"?"d" returns the day of the month; "y" returns the ordinal day in the year. On 29 February 2024, the values are 29 and 60.
"w" and "ww"?"w" returns a weekday position from 1 through 7. "ww" returns a week-of-year value.
No. It returns a numeric part. Use MonthName(DatePart("m", myDate)) or the month name tutorial.
Not when you pass Date() or Now() in server-side Classic ASP. Those functions use the server’s date and time settings.
That is a date calculation, not a DatePart extraction. Construct the day before the first day of the next month with DateSerial(yearNumber, monthNumber + 1, 0), then use Day() if you only need the final day number.
<%
Option Explicit
Dim lastDay
lastDay = DateSerial(2024, 3, 0)
Response.Write Day(lastDay)
%>
The result is 29, because February 2024 has 29 days.
For the documented interval names, defaults and the known week-number limitation, see Microsoft’s DatePart function reference.
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.