VBScript DateDiff function

VBScript DateDiff :

VBScript DateDiff() Function returns the difference between two specified time intervals.

Syntax

DateDiff(interval, date1, date2 [,firstdayofweek[, firstweekofyear]]) 

Parameter Description

ParameterRequired/OptionalExplanation
IntervalRequiredIt takes the values listed in the below table 1
date1RequiredDate both positive and negative parameters
date2RequiredDate both positive and negative parameters
firstdayofweekOptionalSpecifies the first day of the week. It can have all possible values listed in table 2
firstdayofyearOptionalSpecifies the first day of the year. It can have all possible values listed in table 3
Table 1

Below is list of all the possible values of interval parameter -

Interval ValueExplanation
dThis interval value represents a day of the year.
mThis interval value represents a month of the year
yThis interval value represents a year of the year
yyyyThis interval value represents a year
wThis interval value represents a weekday
wwThis interval value represents a week
qThis interval value represents a quarter
hThis interval value represents a hour
mThis interval value represents a minute
sThis interval value represents a second

Table 2 : firstdayofweek Value

Numeric CodeConstantExplanation
0vbUseSystemDayOfWeekUse National Language Support (NLS) API setting
1vbSundaySunday
2vbMondayMonday
3vbTuesdayTuesday
4vbWednesdayWednesday
5vbThursdayThursday
6vbFridayFriday
7vbSaturdaySaturday

Table 3 : firstdayofyear Value

Numeric CodeConstantExplanation
0vbUseSystemUse National Language Support (NLS) API setting
1vbFirstJan1Start with the week in which January 1 occurs (default)
2vbFirstFourDaysStart with the week that has at least four days in the new year
3vbFirstFullWeekStart with the first full week of the new year

Example :

<!DOCTYPE html>
<html>
<body>
<script language="vbscript" type="text/vbscript">
fromDate = "01-Jan-13 00:00:00"
toDate   = "01-Jan-14 23:59:00"
document.write("Ex 1 : " &DateDiff("yyyy",fromDate,toDate) & "<br />")
document.write("Ex 2 : " &DateDiff("q",fromDate,toDate) & "<br />")
document.write("Ex 3 : " &DateDiff("m",fromDate,toDate) & "<br />")
document.write("Ex 4 : " &DateDiff("y",fromDate,toDate) & "<br />")
document.write("Ex 5 : " &DateDiff("d",fromDate,toDate) & "<br />")
document.write("Ex 6 : " &DateDiff("w",fromDate,toDate) & "<br />")
document.write("Ex 7 : " &DateDiff("ww",fromDate,toDate)& "<br />")
document.write("Ex 8 : " &DateDiff("h",fromDate,toDate) & "<br />")
document.write("Ex 9 : " &DateDiff("n",fromDate,toDate) & "<br />")
document.write("Ex 10 : "&DateDiff("s",fromDate,toDate) & "<br />")
</script>
</body>
</html>

Output :

After running above script in IE we will get following output -

Ex 1 : 1
Ex 2 : 4
Ex 3 : 12
Ex 4 : 365
Ex 5 : 365
Ex 6 : 52
Ex 7 : 52
Ex 8 : 8783
Ex 9 : 527039
Ex 10 : 31622340