SQL PHP HTML ASP JavaScript articles and free scripts to download
 

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.
Further readings
Getting todays Date and Time based on local settings
Formatting date and time on local settings
Adding Date intervals using DateAdd function
Finding difference between two dates using DateDiff function
Collecting parts of Date by using DatePart function
Collecting Month name by using Month number by MonthName function
Collecting Name of the weekday of any Date by WeekDayName function
Generating Date variables by using Month, Day and Year Value
Function IsDate to check valid date or date variable
Checking user entered date value
 
Scripts
PHP
JavaScript
All ASP Tutorials
Popular Tutorials
Managing two drop downs
ASP Tutorials
Date and time
Declaring array
Form in ASP
Server.MapPath
Date Time & MSSQL
Select Query
File System Object
Subscribe
Submit your email address and receive article and product notifications. Your email is safe with us.