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
| Parameter | Required/Optional | Explanation |
|---|---|---|
| Interval | Required | It takes the values listed in the below table 1 |
| date1 | Required | Date both positive and negative parameters |
| date2 | Required | Date both positive and negative parameters |
| firstdayofweek | Optional | Specifies the first day of the week. It can have all possible values listed in table 2 |
| firstdayofyear | Optional | Specifies the first day of the year. It can have all possible values listed in table 3 |
Below is list of all the possible values of interval parameter -
| Interval Value | Explanation |
|---|---|
| d | This interval value represents a day of the year. |
| m | This interval value represents a month of the year |
| y | This interval value represents a year of the year |
| yyyy | This interval value represents a year |
| w | This interval value represents a weekday |
| ww | This interval value represents a week |
| q | This interval value represents a quarter |
| h | This interval value represents a hour |
| m | This interval value represents a minute |
| s | This interval value represents a second |
Table 2 : firstdayofweek Value
| Numeric Code | Constant | Explanation |
|---|---|---|
| 0 | vbUseSystemDayOfWeek | Use National Language Support (NLS) API setting |
| 1 | vbSunday | Sunday |
| 2 | vbMonday | Monday |
| 3 | vbTuesday | Tuesday |
| 4 | vbWednesday | Wednesday |
| 5 | vbThursday | Thursday |
| 6 | vbFriday | Friday |
| 7 | vbSaturday | Saturday |
Table 3 : firstdayofyear Value
| Numeric Code | Constant | Explanation |
|---|---|---|
| 0 | vbUseSystem | Use National Language Support (NLS) API setting |
| 1 | vbFirstJan1 | Start with the week in which January 1 occurs (default) |
| 2 | vbFirstFourDays | Start with the week that has at least four days in the new year |
| 3 | vbFirstFullWeek | Start 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
