Go to: Articles List

Date Manipulation Part 2 : Adding & Subtracting Dates & Times
Part 1 | Part 2 | Part 3 | Part 4
VBScript provides developers with a wonderful function called DateAdd(). With this function you can add or subtract from a date or time.

DateAdd(PartOfDate, AmountToChange, Date)
- returns a date that has been changed the specified amount
- AmountToChange is the number to which you want the date to change (1, -4, 10, etc).
- Date is a valid date
- PartOfDate parameter can have the following values:
yyyy - Year
q - Quarter
m - Month
y - Day of year
d - Day
w - Weekday
ww - Week of year
h - Hour
n - Minute
s - Second

An example of what each of those values will output is:

var1 = Now()
Response.Write("var1 = " & var1 & "<br>")
Response.Write("var1 plus 10 days = " & DateAdd("d", 10, var1) & "<br>")
Response.Write("var1 minus 10 days = " & DateAdd("d", -10, var1) & "<br>")
Response.Write("var1 plus 3 months = " & DateAdd("m", 3, var1) & "<br>")
Response.Write("var1 minus 3 months = " & DateAdd("m", -3, var1) & "<br>")
Response.Write("var1 plus 2 hours = " & DateAdd("h", 2, var1) & "<br>")
Response.Write("var1 minus 2 hours = " & DateAdd("h", -2, var1) & "<br>")

Results:
var1 = 9/1/99 1:41:04 PM
var1 plus 10 days = 9/11/99 1:41:04 PM
var1 minus 10 days = 8/22/99 1:41:04 PM
var1 plus 3 months = 12/1/99 1:41:04 PM
var1 minus 3 months = 6/1/99 1:41:04 PM
var1 plus 2 hours = 9/1/99 3:41:04 PM
var1 minus 2 hours = 9/1/99 11:41:04 AM