Build Date values with DateSerial() in Classic ASP

DateSerial() creates a real VBScript Date value from separate year, month and day numbers. This is safer than building a locale-sensitive date string when form fields already provide numeric components.

Do not place variable names inside a date literal

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

Correct syntax

DateSerial(year, month, day)

Create February 28, 2007

Dim d1, d2, d3
d1 = 2007
d2 = 2
d3 = 28
Response.Write DateSerial(d1, d2, d3)

Move to the next day

Response.Write DateSerial(d1, d2, d3 + 1)

One month earlier

Response.Write DateSerial(d1, d2 - 1, d3)

Five years later

Response.Write DateSerial(d1 + 5, d2, d3)

Leap-year behavior

Dim d1, d2, d3
d1 = 2008
d2 = 2
d3 = 28
Response.Write DateSerial(d1, d2, d3 + 1)

The result is February 29, 2008.

Validation note: DateSerial() intentionally normalizes out-of-range components. For example, day 32 rolls into the following month. When validating form input, compare Year(), Month() and Day() of the constructed date with the original numeric inputs.

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