Go to: Articles List

Date Manipulation Part 3 : Find the amount of time between dates with DateDiff()
Part 1 | Part 2 | Part 3 | Part 4
Find out how much time occurs between two dates is easy with DateDiff(). This function is useful if you need to find the amount of time between one date and another, or if you need to find out how much time is left until the end of the year.

DateDiff(PartOfDate, Date1, Date2[, FirstDayOfWeek, FirstDayOfYear])
- returns the amount of time in between the two dates in the form of PartOfDate, therefore if you set PartOfDate equal to year, the function will tell you how many years are in between Date1 and Date2 even if you are comparing Dec 31 and Jan 1. If you set PartOfDate equal to day then the function will return to you the number of days between Date1 and Date2.
- if Date1 occurs after Date2 the function will return to you a negative number.
- optional parameters are setting the first day of the week (defaults to Sunday), and setting the first week of the year (defaults to Jan 1).

FirstDayOfWeek optional parameter can have one of the following values:
0 - Use National Language Support API setting
1 - Sunday (the default value if this parameter is not supplied)
2 - Monday
3 - Tuesday
4 - Wednesday
5 - Thursday
6 - Friday
7 - Saturday

FirstDayOfYear optional parameter can have one of the following values:
0 - Use National Language Support API setting
1 - Start with the week that has Jan 1 in it (the default value if this parameter is not supplied)
2 - Start with the first week that contains at least four days in the new year.
3 - Start with the first week that is a full week in the new year.

- I recommend just leaving the two optional parameters out unless they specifically apply to what you are currently trying to do.

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

Example of use:

var1 = "04/04/1999"<br> var2 = "01/11/2000"<br>

Response.Write("var1 = " & var1 & "<br>")
Response.Write("var2 = " & var2 & "<br>")
Response.Write("var1 to var2 is " & DateDiff("d", var1, var2) & " days <br>")
Response.Write("var1 to var2 is " & DateDiff("m", var1, var2) & " months <br>")
Response.Write("var1 to var2 is " & DateDiff("yyyy", var1, var2) & " year(s) <br>")

Results:
var1 = 04/04/1999
var2 = 01/11/2000
var1 to var2 is 282 days
var1 to var2 is 9 months
var1 to var2 is 1 year(s)