Adding / subtracting to a date / time

Thankfully this is an easy task with VBScript thanks to the DateAdd() function , lets take a look at some examples . In all cases we will take the date which is now and add / subtract from it.

Code :

<%
Dim theDate
theDate = Now()
Response.Write "The date / time is " & theDate & "<br>"
Response.Write "Add 12 hours " & DateAdd("h" , 12 , theDate) & "<br>"
Response.Write "Subtract 12 hours " & DateAdd("h" , -12 , theDate) & "<br>"
Response.Write "Add 1 month " & DateAdd("m" , 1 , theDate) & "<br>"
Response.Write "Subtract 1 month " & DateAdd("m" , -1 , theDate) & "<br>"
%>

 

Notes :

You can use the following for the first parameter which determines what you are changing the date by .

yyyy - Year
q - Quarter
m - Month
y - Day of year
d - Day
w - Weekday
ww - Week of year
h - Hour
n - Minute
s - Second

The second parameter is the number you want the date to change by , a positive number will make the date / time go forward and a negative number will make the date / time go back.

Sponsors