Generating Date variables by using DateSerial function in ASP
By dateserial function in VBScript used in ASP we can generate date variable by using different variables for day, month and year. This is a quite useful as we can take different user input and create a date variable out of it. We can't create a date variable by placing the variables within two # marks. Like this
My_date=#my_month, my_day,1985#
The above code will give error message. Let us start with the syntax of dateserial function.
DateSerial(yyyy, dd, mm)
The above function takes three inputs month, date and year and generates a date variable. The date variable then can be used for normal operations. Here is an example of generating date variable.
Dim d1, d2, d3
d1="2007"
d2="2"
d3="28"
Response.Write DateSerial(d1,d2,d3)
The above code will return 2/28/2007.
DateSerial can be used to generate different dates before or after the day entered. Here is an example to get next day of the given date.
Response.Write DateSerial(d1,d2,d3+1)
Same way we can get the date variable for one month before the dates like this
Response.Write DateSerial(d1,d2-1,d3)
For five years advance date, here it is
Response.Write DateSerial(d1+5,d2,d3)
DateSerial function takes care of leap years also. Check this code again
Dim d1, d2, d3
d1="2007"
d2="2"
d3="28"
Response.Write DateSerial(d1,d2,d3+1)
We will get 3/1/2007 as output but if we change the value of year to 2008 ( d1="2008") then the out put will change to 2/29/2008
DateSerial function is mostly used to collect user enter data and generating a date variable out of it.
← ASP Home
This article is written by plus2net.com team.
plus2net.com
✖
We use cookies to improve your browsing experience. . Learn more