DateDiff Function
DateDiff Function returns the number of time intervals between two dates.
Syntax
DateDiff(interval,date1,date2[,firstdayofweek[,firstweekofyear]])
| Parameter | Description |
|---|---|
| interval | Required. The unit of time used to calculate the difference between date1 and date2. Can be one of 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 |
| date1,date2 | Required. Date expressions. The two dates to be used in the calculation. |
| firstdayofweek | Optional. Specifies the first day of the week. If not specified, vbSunday is assumed. Can be one of the following values: * 0 = vbUseSystemDayOfWeek - Use NLS API setting * 1 = vbSunday - Sunday (default) * 2 = vbMonday - Monday * 3 = vbTuesday - Tuesday * 4 = vbWednesday - Wednesday * 5 = vbThursday - Thursday * 6 = vbFriday - Friday * 7 = vbSaturday - Saturday |
| firstweekofyear | Optional. Specifies the first week of the year. If not specified, vbFirstJan1 is assumed. Can be one of the following values: * 0 = vbUseSystem - Use NLS API setting * 1 = vbFirstJan1 - Week in which January 1 occurs (default) * 2 = vbFirstFourDays - Week that has at least four days in the new year * 3 = vbFirstFullWeek - First full week of the year |
Examples
Example 1
Difference between January 31, 2009 and January 31, 2010:
fromDate="31-Jan-09 00:00:00" toDate="31-Jan-10 23:59:00" document.write(DateDiff("yyyy",fromDate,toDate) & "") document.write(DateDiff("q",fromDate,toDate) & "
") document.write(DateDiff("m",fromDate,toDate) & "
") document.write(DateDiff("y",fromDate,toDate) & "
") document.write(DateDiff("d",fromDate,toDate) & "
") document.write(DateDiff("w",fromDate,toDate) & "
") document.write(DateDiff("ww",fromDate,toDate) & "
") document.write(DateDiff("h",fromDate,toDate) & "
") document.write(DateDiff("n",fromDate,toDate) & "
") document.write(DateDiff("s",fromDate,toDate) & "
")
The above example outputs:
1
4
12
365
365
52
53
8783
527039
31622340
Example 2
How many weeks between December 31, 2009 and December 31, 2012 (starting on Monday):
fromDate=CDate("2009/12/31") toDate=CDate("2012/12/31") document.write(DateDiff("w",fromDate,toDate,vbMonday))The above example outputs:
156
YouTip