SQL PHP HTML ASP JavaScript articles and free scripts to download
 

DateAdd function to add date and time

We can add date by using DateAdd function of VBScript using in ASP. We can add any type of value like day, month , year etc using DateAdd function. Here is the syntax for the DateAdd function.

DateAdd ( date_type, increment_by, date)

Here date_type is the type of date to be added. It can be month, year, Day, Hour, minute etc. Different values are available in a table at the end of this tutorial. The argument increment_by is the interval we want to add to the third argument date.

Here is the code explaining the use of DateAdd function.

Dim d_today,d_added
d_today=Date
Response.Write "Today date is " & d_today & "<br>"
d_added=DateAdd("yyyy",5,d_today)
Response.Write "Add 5 Years " & d_added & "<br>"

In the above code we have added five years to the current date. By changing the value of date_type we can add month, days etc to the present date. Here is the code to add 6 months to today.

d_added=DateAdd("m",6,d_today)

DateAdd function will not return any invalid date. Let us try with this example.

d_added=DateAdd("d",32,#1/28/04#)

This line will return 2/29/2004,

Now let us change the year to 2003 and see the result

d_added=DateAdd("d",32,#1/28/03#)

The output of above line is 3/1/2003
This way we can add different types of dates to an existing date & time.
Table for different date_type values
ValueMeaning
"yyyy"Year
"m"Month
"d"Day
"h"Hour
"n"Minute
"s"Second
"w"Weekday
"ww"Week of the year
"q"Quarter



Post Comment This is for short comments only. Use the forum for more discussions.
Name
Email( not to be displayed)Privacy Policy
1+2=This is to prevent automatic submission by spammers. Please enter the result of the sum as asked


Join Our Email List
Email:  
For Email Newsletters you can trust
Date & Time