VBScript DateAdd Function

Advertisements


DateAdd :

A Function, which returns a date to which a specified time interval has been added.

Syntax :

DateAdd(interval,number,date) 

Parameter Description :

  1. Interval, a Required Parameter. It can take the following values:

    • d - day of the year.

    • m - month of the year

    • y - year of the year

    • yyyy - year

    • w - weekday

    • ww - week

    • q - quarter

    • h - hour

    • m - minute

    • s - second

  2. Number, a Required parameter. It can take both positive and negative parameters.

  3. Date, a Required parameter. A Variant or literal representing the date to which interval is added.

Example

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

' Positive Interal
date1=01-Jan-2013
document.write("Line 1 : " &DateAdd("yyyy",1,date1) & "<br />")
document.write("Line 2 : " &DateAdd("q",1,date1) & "<br />")
document.write("Line 3 : " &DateAdd("m",1,date1) & "<br />")
document.write("Line 4 : " &DateAdd("y",1,date1) & "<br />")
document.write("Line 5 : " &DateAdd("d",1,date1) & "<br />")
document.write("Line 6 : " &DateAdd("w",1,date1) & "<br />")
document.write("Line 7 : " &DateAdd("ww",1,date1) & "<br />")
document.write("Line 8 : " &DateAdd("h",1,"01-Jan-2013 12:00:00") & "<br />")
document.write("Line 9 : " &DateAdd("n",1,"01-Jan-2013 12:00:00") & "<br />")
document.write("Line 10 : "&DateAdd("s",1,"01-Jan-2013 12:00:00") & "<br />")

' Negative Interval
document.write("Line 11 : " &DateAdd("yyyy",-1,date1) & "<br />")
document.write("Line 12 : " &DateAdd("q",-1,date1) & "<br />")
document.write("Line 13 : " &DateAdd("m",-1,date1) & "<br />")
document.write("Line 14 : " &DateAdd("y",-1,date1) & "<br />")
document.write("Line 15 : " &DateAdd("d",-1,date1) & "<br />")
document.write("Line 16 : " &DateAdd("w",-1,date1) & "<br />")
document.write("Line 17 : " &DateAdd("ww",-1,date1) & "<br />")
document.write("Line 18 : " &DateAdd("h",-1,"01-Jan-2013 12:00:00") & "<br />")
document.write("Line 19 : " &DateAdd("n",-1,"01-Jan-2013 12:00:00") & "<br />")
document.write("Line 20 : " &DateAdd("s",-1,"01-Jan-2013 12:00:00") & "<br />") 
</script>
</body>
</html>

When you save it as .html and execute it in Internet Explorer, then the above script will produce the following result:

Line 1 : 27/06/1895
Line 2 : 27/09/1894
Line 3 : 27/07/1894
Line 4 : 28/06/1894
Line 5 : 28/06/1894
Line 6 : 28/06/1894
Line 7 : 4/07/1894
Line 8 : 1/01/2013 1:00:00 PM
Line 9 : 1/01/2013 12:01:00 PM
Line 10 : 1/01/2013 12:00:01 PM
Line 11 : 27/06/1893
Line 12 : 27/03/1894
Line 13 : 27/05/1894
Line 14 : 26/06/1894
Line 15 : 26/06/1894
Line 16 : 26/06/1894
Line 17 : 20/06/1894
Line 18 : 1/01/2013 11:00:00 AM
Line 19 : 1/01/2013 11:59:00 AM
Line 20 : 1/01/2013 11:59:59 AM


Advertisements
Advertisements