VBScript DatePart() Function in Classic ASP with Examples

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.

On this page — Show page sectionsHide page sections

DatePart() Syntax and Parameters

The function returns an integer containing the requested portion of a date value.

DatePart(interval, date[, firstdayofweek[, firstweekofyear]])
ParameterRequired?Purpose
intervalYesA string such as "yyyy" (year), "m" (month) or "ww" (week number).
dateYesA VBScript date value, such as Date(), Now() or DateSerial(2026, 11, 12).
firstdayofweekNoControls the first weekday when using "w" or "ww"; Sunday is the default.
firstweekofyearNoControls 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.

DatePart() Interval Codes

All ten interval codes supported by VBScript are listed below.

IntervalExtractsExample
yyyyYearDatePart("yyyy", d)
qQuarter (1–4)DatePart("q", d)
mMonth (1–12)DatePart("m", d)
yDay of year (1–365, or 366)DatePart("y", d)
dDay of month (1–31)DatePart("d", d)
wDay of week (1–7)DatePart("w", d)
wwWeek of yearDatePart("ww", d)
hHour (0–23)DatePart("h", d)
nMinute (0–59)DatePart("n", d)
sSecond (0–59)DatePart("s", d)
Two easy mix-ups: "d" returns the day within the month; "y" returns the day within the year. "w" means weekday; "ww" means week number.

Practical DatePart() Examples

1. Extract the year, month and day from a fixed date

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#)

2. Find the quarter and day number within the year

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.

3. Extract hour, minute and second

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())

4. Display the current year and month in a report heading

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.

Weekday and Week Number Calculations

Get the weekday using a Monday-first week

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.

Choose the first week of the year

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
%>
SettingMeaning
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.
Year-end caution: Microsoft documents a week-number issue for 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.

DatePart() Compared with Day(), Month() and Year()

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.

Working with Date Input in Classic ASP

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.

Practical Uses and Limitations

  • Monthly reports: Group a display or report heading by the result of DatePart("m", ...) and DatePart("yyyy", ...).
  • Quarterly summaries: Use DatePart("q", ...) to identify a calendar quarter.
  • Daily dashboards: Use "d" and "y" when you need day-of-month and day-of-year respectively.
  • Schedule displays: Set firstdayofweek explicitly when weekdays must follow a chosen convention.
  • Week-based reports: Choose the week rule explicitly and test year boundaries.

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.

Common Errors and Troubleshooting

ProblemCauseSolution
Type mismatchThe date argument is not recognized as a date.Use IsDate() before CDate(), or create a date using DateSerial().
Minute returns a month-like valueUsed "m" rather than "n".Use "n" for minutes.
Unexpected weekday numberSunday-first default differs from the expected Monday-first convention.Pass vbMonday as the third argument.
Unexpected week numberWeek-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 textLocale-dependent interpretation of a date string.Build a known date with DateSerial(year, month, day).
Time components return zeroThe value contains a date but no time, such as Date().Use Now() or a date/time value containing a time component.

Frequently Asked Questions

What is the difference between "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.

What is the difference between "w" and "ww"?

"w" returns a weekday position from 1 through 7. "ww" returns a week-of-year value.

Can DatePart() return a month name?

No. It returns a numeric part. Use MonthName(DatePart("m", myDate)) or the month name tutorial.

Does DatePart() use the visitor’s local time?

Not when you pass Date() or Now() in server-side Classic ASP. Those functions use the server’s date and time settings.

How do I get the last day of a month?

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.


ASP Home






✖
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
© 2000-2026 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer