VBScript DateAdd function



VBScript DateAdd

VBScript DateAdd() returns a date to which a specified time interval has been added.

Syntax

DateAdd(interval,number,date) 

Parameter Description

Parameter Required/Optional Explanation
Interval Required It takes the values listed in the below table
Number Required It takes both positive and negative parameters
Date Required It is variable or literal which represents a date to be added

Below is list of all the possible values of interval parameter –

Interval Value Explanation
d This interval value represents a day of the year.
m This interval value represents a month of the year
y This interval value represents a year of the year
yyyy This interval value represents a year
w This interval value represents a weekday
ww This interval value represents a week
q This interval value represents a quarter
h This interval value represents a hour
m This interval value represents a minute
s This interval value represents a second

Example

Example 1 : Passing Positive Parameter

<!DOCTYPE html>
<html>
<body>
<script language="vbscript" type="text/vbscript">

document.write(DateAdd("yyyy",1,"10-Mar-14") & "<br>")
document.write(DateAdd("q",1,"10-Mar-14") & "<br>")
document.write(DateAdd("m",1,"10-Mar-14") & "<br>")
document.write(DateAdd("y",1,"10-Mar-14") & "<br>")
document.write(DateAdd("d",1,"10-Mar-14") & "<br>")
document.write(DateAdd("w",1,"10-Mar-14") & "<br>")
document.write(DateAdd("ww",1,"10-Mar-14") & "<br>")
document.write(DateAdd("h",1,"10-Mar-14 01:10:00") & "<br>")
document.write(DateAdd("n",1,"10-Mar-14 01:10:00") & "<br>")
document.write(DateAdd("s",1,"10-Mar-14 01:10:00") & "<br>")

</script>

</body>
</html>

Output :

After running above script in IE we will get following output –

3/10/2015
6/10/2014
4/10/2014
3/11/2014
3/11/2014
3/11/2014
3/17/2014
3/10/2014 2:10:00 AM
3/10/2014 1:11:00 AM
3/10/2014 1:10:01 AM

Example 2 : Passing Negative Parameter

<!DOCTYPE html>
<html>
<body>
<script language="vbscript" type="text/vbscript">

document.write(DateAdd("yyyy",-1,"10-Mar-14") & "<br>")
document.write(DateAdd("q",-1,"10-Mar-14") & "<br>")
document.write(DateAdd("m",-1,"10-Mar-14") & "<br>")
document.write(DateAdd("y",-1,"10-Mar-14") & "<br>")
document.write(DateAdd("d",-1,"10-Mar-14") & "<br>")
document.write(DateAdd("w",-1,"10-Mar-14") & "<br>")
document.write(DateAdd("ww",-1,"10-Mar-14") & "<br>")
document.write(DateAdd("h",-1,"10-Mar-14 01:10:00") & "<br>")
document.write(DateAdd("n",-1,"10-Mar-14 01:10:00") & "<br>")
document.write(DateAdd("s",-1,"10-Mar-14 01:10:00") & "<br>")

</script>

</body>
</html>

Output :

After running above script in IE we will get following output –

3/10/2013
12/10/2013
2/10/2014
3/9/2014
3/9/2014
3/9/2014
3/3/2014
3/10/2014 12:10:00 AM
3/10/2014 1:09:00 AM
3/10/2014 1:09:59 AM