VBScript
DateDiff
function
The VBScript DateDiff function returns the interval between two dates.
Complete VBScript reference manual
The DateDiff function returns the number of time intervals between the two dates.
Grammar
DateDiff(interval,date1,date2[,firstdayofweek[,firstweekofyear]])
Parameters
parameter
|
describe
|
interval
|
Required.Calculate the unit of time interval between Date1 and Date2.
The following value can be used:
-
YYYY - year
-
Q - quarter
-
M - month
-
Y - the first day of the year
-
D - day
-
W - as the first few days
-
WW - the last year
-
H - hour
-
n - points
-
s - second
|
date1,date2
|
Required.Date expression.Two dates which need to be used in the calculation.
|
firstdayofweek
|
Optional.The number of days in the week, that is, the first few days of the week.
The following value can be used:
-
0 = VbusesystemDayofweek - Using Regional Language Support (NLS) API Settings
-
1 = VBSunday - Sunday (default)
-
2 = VBMONDAY - Monday
-
3 = VBTESDAY - Tuesday
-
4 = vbwednesday - Wednesday
-
5 = VBTHURSDAY - Thursday
-
6 = VBFRIDAY - Friday
-
7 = VBSATURDAY - Saturday
|
firstweekofyear
|
Optional.The first week of the year is specified.
The following value can be used:
-
0 = VbuseSystem - Use Regional Language Support (NLS) API Settings
-
1 = VBFIRSTJAN1 - starting from January 1st (default)
-
2 = vbfirstfourDays - starting with at least four days in the new year
-
3 = vbfirstfullweek - starting with the first complete week in the new year
|
Instance 1
Differences between January 31, 2009 and January 31, 2010:
<script type="text/vbscript">
fromDate="31-Jan-09 00:00:00"
toDate="31-Jan-10 23:59:00"
document.write(DateDiff("yyyy",fromDate,toDate) & "<br />")
document.write(DateDiff("q",fromDate,toDate) & "<br />")
document.write(DateDiff("m",fromDate,toDate) & "<br />")
document.write(DateDiff("y",fromDate,toDate) & "<br />")
document.write(DateDiff("d",fromDate,toDate) & "<br />")
document.write(DateDiff("w",fromDate,toDate) & "<br />")
document.write(DateDiff("ww",fromDate,toDate) & "<br />")
document.write(DateDiff("h",fromDate,toDate) & "<br />")
document.write(DateDiff("n",fromDate,toDate) & "<br />")
document.write(DateDiff("s",fromDate,toDate) & "<br />")
</script>
The above example output results:
1
4
12
365
365
52
53
8783
527039
31622340
Try it out . . .
Instance 2
What are the weeks between December 31, 2009 and December 31, 2012 (starting Monday):
<script type="text/vbscript">
fromDate=CDate("2009/12/31")
toDate=CDate("2012/12/31")
document.write(DateDiff("w",fromDate,toDate,vbMonday))
</script>
The above example output results:
156
Try it out . . .
Complete VBScript reference manual