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.
DateSerial(year, month, day)
| Argument | Description |
|---|---|
year | Prefer a complete four-digit year to avoid two-digit-year interpretation |
month | Month component; numbers outside 1–12 are normalized |
day | Day component; out-of-range days roll into adjoining months |
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.
<%
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
<%
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
<%
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.
<%
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
<%
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.
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
DateSerial(). See complete date-input validation.<%
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
It accepts numeric components, avoiding text-order ambiguity. Displaying the returned Date is still locale-dependent.
The function deliberately normalizes overflow into March. To validate a user date, compare the original components with the result.
Avoid it. Two-digit years can be interpreted under platform settings; use four-digit years.
Use DateAdd("m", 1, myDate) for month arithmetic.
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.