DateDiff() between two dates in Classic ASP

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.

On this page — Show page sectionsHide page sections

Syntax and arguments

DateDiff(interval, date1, date2[, firstdayofweek[, firstweekofyear]])
ParameterExplanation
intervalUnit being counted, such as "d", "m", "yyyy"
date1Starting Date value
date2Ending Date value
firstdayofweekOptional beginning of the week for weekly calculations
firstweekofyearOptional rule for determining week 1

DateDiff interval codes

CodeMeaning
yyyyYear boundaries
qQuarter boundaries
mMonth boundaries
y, dDay boundaries
wWeekday-related interval count; not working-day count
wwCalendar week boundaries
hHour boundaries
nMinute boundaries
sSecond boundaries

Original example: difference in months

<%
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.

Count days and reverse the date order

<%
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

Year and month boundaries versus elapsed time

<%
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
Important: 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.

Compare hours and minutes

<%
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.

Days remaining until a deadline

<%
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.

Week numbering and weekends

<%
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.

Common errors and misconceptions

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().

Frequently asked questions

Why is DateDiff("yyyy") equal to 1 for December 31 to January 1?

Because it counts calendar-year boundaries, not completed 365-day periods.

How do I obtain a negative difference?

Put the later date first and the earlier date second.

Does "w" count Monday-to-Friday business days?

No. It is not a business-day calculation. Write a separate rule that accounts for weekdays and holidays.

How can I extract the month of one date?

Use DatePart("m", myDate) or Month(myDate).

Back to top ↑


ASP Home






✖
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
© 2000-2026 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer