DateDiff() measures the number of selected calendar or time interval boundaries between two dates in Classic ASP. It can return positive or negative results, depending on the order of the dates.
<%
Dim startDate, endDate
startDate = DateSerial(2024, 2, 28)
endDate = DateSerial(2024, 3, 2)
Response.Write DateDiff("d", startDate, endDate)
%>
Output: 3 days. This includes February 29, because 2024 was a leap year.
DateDiff(interval, date1, date2[, firstdayofweek[, firstweekofyear]])
| Parameter | Explanation |
|---|---|
interval | Unit being counted, such as "d", "m", "yyyy" |
date1 | Starting Date value |
date2 | Ending Date value |
firstdayofweek | Optional beginning of the week for weekly calculations |
firstweekofyear | Optional rule for determining week 1 |
| Code | Meaning |
|---|---|
yyyy | Year boundaries |
q | Quarter boundaries |
m | Month boundaries |
y, d | Day boundaries |
w | Weekday-related interval count; not working-day count |
ww | Calendar week boundaries |
h | Hour boundaries |
n | Minute boundaries |
s | Second boundaries |
<%
Dim d_diff
d_diff = DateDiff("m", #2/28/2003#, #2/28/2006#)
Response.Write "Difference in months: " & d_diff
%>
Difference in months: 36
This counts 36 calendar-month boundaries. It does not calculate months by dividing an absolute number of days by 30.
<%
Dim firstDate, secondDate
firstDate = DateSerial(2024, 1, 1)
secondDate = DateSerial(2024, 1, 11)
Response.Write DateDiff("d", firstDate, secondDate) & "<br>"
Response.Write DateDiff("d", secondDate, firstDate)
%>
10
-10
<%
Dim d1, d2
d1 = DateSerial(2025, 12, 31)
d2 = DateSerial(2026, 1, 1)
Response.Write "Years: " & DateDiff("yyyy", d1, d2) & "<br>"
Response.Write "Months: " & DateDiff("m", d1, d2) & "<br>"
Response.Write "Days: " & DateDiff("d", d1, d2)
%>
Years: 1
Months: 1
Days: 1
DateDiff("yyyy",...) returning 1 does not mean that a full year elapsed. It counts crossing into a different calendar year. Use the measure appropriate to your application.<%
Dim startTime, endTime
startTime = DateSerial(2026, 11, 13) + TimeSerial(9, 15, 0)
endTime = DateSerial(2026, 11, 13) + TimeSerial(11, 45, 0)
Response.Write "Hours: " & DateDiff("h", startTime, endTime) & "<br>"
Response.Write "Minutes: " & DateDiff("n", startTime, endTime)
%>
Hours: 2
Minutes: 150
Hours count hour boundaries; minutes count minute boundaries. For precise elapsed durations, use a consistent time basis and take care with time zones and daylight saving changes.
<%
Dim dueDate, daysRemaining
dueDate = DateSerial(2026, 12, 31)
daysRemaining = DateDiff("d", Date, dueDate)
If daysRemaining >= 0 Then
Response.Write daysRemaining & " days remain"
Else
Response.Write "Deadline passed " & Abs(daysRemaining) & " days ago"
End If
%>
This result depends on the server date. For a deadline that includes a time, use Now and select suitable time units instead of relying only on calendar days.
<%
Dim d1, d2
d1 = DateSerial(2026, 11, 1)
d2 = DateSerial(2026, 11, 30)
Response.Write DateDiff("ww", d1, d2, vbMonday, vbFirstFourDays)
%>
The optional week arguments change which boundaries are counted. "ww" is not a function for counting workdays or business weeks.
Do not treat "m" as minutes; minutes use "n". Avoid treating year/month boundary counts as precise elapsed anniversaries. For date arithmetic rather than interval counting, see DateAdd().
Because it counts calendar-year boundaries, not completed 365-day periods.
Put the later date first and the earlier date second.
No. It is not a business-day calculation. Write a separate rule that accounts for weekdays and holidays.
Use DatePart("m", myDate) or Month(myDate).
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.