Build Date values with DateSerial() in Classic ASP

The DateSerial(year, month, day) function builds a real VBScript Date from separate numeric components. It avoids ambiguous date strings and is especially useful for handling values collected from form controls.

<%
Dim myDate
myDate = DateSerial(2007, 2, 28)
Response.Write Year(myDate) & "-" & Month(myDate) & "-" & Day(myDate)
%>

Output: 2007-2-28. These are separate numeric parts; for leading-zero formatting, see the date formatting tutorial.

On this page — Show page sectionsHide page sections

Syntax and arguments

DateSerial(year, month, day)
ArgumentDescription
yearPrefer a complete four-digit year to avoid two-digit-year interpretation
monthMonth component; numbers outside 1–12 are normalized
dayDay component; out-of-range days roll into adjoining months

Incorrect date literal with variable names

The original tutorial warned against placing variable names inside a #...# date literal. That does not substitute the variables:

' Incorrect: variables are not evaluated inside a #...# date literal
my_date = #my_month, my_day, 1985#

Pass the variables to DateSerial() instead.

Build a date from three variables

<%
Dim d1, d2, d3, myDate
d1 = 2007
d2 = 2
d3 = 28
myDate = DateSerial(d1, d2, d3)
Response.Write Day(myDate) & "/" & Month(myDate) & "/" & Year(myDate)
%>
28/2/2007

Move to the next day

<%
Dim d1, d2, d3
d1 = 2007: d2 = 2: d3 = 28
Response.Write Month(DateSerial(d1, d2, d3 + 1)) & "/" & _
  Day(DateSerial(d1, d2, d3 + 1)) & "/" & Year(DateSerial(d1, d2, d3 + 1))
%>
3/1/2007

One month earlier and five years later

<%
Dim d1, d2, d3
d1 = 2007: d2 = 2: d3 = 28
Response.Write DateSerial(d1, d2 - 1, d3) & "<br>"
Response.Write DateSerial(d1 + 5, d2, d3)
%>

These two expressions preserve the original examples. Date values are displayed according to the ASP locale.

Leap-year behavior

<%
Dim leapDate, nonLeap
leapDate = DateSerial(2008, 2, 28 + 1)
nonLeap = DateSerial(2007, 2, 28 + 1)
Response.Write Month(leapDate) & "/" & Day(leapDate) & "/" & Year(leapDate) & "<br>"
Response.Write Month(nonLeap) & "/" & Day(nonLeap) & "/" & Year(nonLeap)
%>
2/29/2008
3/1/2007

Find the last day of any month

<%
Dim yearNo, monthNo, lastDay
yearNo = 2024
monthNo = 2
lastDay = DateSerial(yearNo, monthNo + 1, 0)
Response.Write Day(lastDay)
%>
29

Day zero of the following month means the final day of the requested month. This works for December too, when the next month is in the following year.

Safely validate year, month and day input

DateSerial() automatically normalizes invalid combinations: DateSerial(2024, 2, 31) becomes a date in March. For form validation, verify numeric ranges and compare the resulting components to the submitted values:

<%
Dim y, m, d, candidate, validDate
y = 2024: m = 2: d = 31
validDate = False
If y >= 1000 And y <= 9999 And m >= 1 And m <= 12 And d >= 1 And d <= 31 Then
  candidate = DateSerial(y, m, d)
  validDate = (Year(candidate) = y And Month(candidate) = m And Day(candidate) = d)
End If
Response.Write validDate
%>
False
Do not use normalization as validation. Compare all three components, and validate that form data contains integers before calling DateSerial(). See complete date-input validation.

Dates across year boundaries

<%
Dim lastYear, nextYear
lastYear = DateSerial(2026, 12, 31)
nextYear = DateSerial(2026, 12, 32)
Response.Write Year(lastYear) & "-" & Month(lastYear) & "-" & Day(lastYear) & "<br>"
Response.Write Year(nextYear) & "-" & Month(nextYear) & "-" & Day(nextYear)
%>
2026-12-31
2027-1-1

Frequently asked questions

Is DateSerial() affected by regional formats?

It accepts numeric components, avoiding text-order ambiguity. Displaying the returned Date is still locale-dependent.

Why does February 31 not raise an error?

The function deliberately normalizes overflow into March. To validate a user date, compare the original components with the result.

Can I use a two-digit year?

Avoid it. Two-digit years can be interpreted under platform settings; use four-digit years.

How can I add one month?

Use DateAdd("m", 1, myDate) for month arithmetic.

Back to top ↑


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