VBScript DateDiff Function
Definition and Usage
The DateDiff function can return the number of time intervals between two dates.
The DateDiff function is used to calculate the difference between two date-time values, using the method date2 - date1.
When comparing years, ignore the values below the month; when comparing months, ignore the values below the day, and so on.
Note:The firstdayofweek parameter affects the calculations using the interval symbols 'w' and 'ww'.
Syntax
DateDiff(interval,date1,date2[,firstdayofweek[,firstweekofyear]])
Parameters | Description |
---|---|
interval |
Required. The unit of time interval to be used in calculating the difference between date1 and date2. The following values can be used:
|
date1,date2 | Required. Date expression. Two dates to be used in the calculation. |
firstdayofweek |
Optional. Defines the number of days in a week, i.e., the day of the week. The following values can be used:
|
firstweekofyear |
Optional. Defines the first week of the year. The following values can be used:
|
Esempio
Esempio 1
document.write(Date & "<br />") document.write(DateDiff("m",Date,"12/31/2002") & "<br />") document.write(DateDiff("d",Date,"12/31/2002") & "<br />") document.write(DateDiff("n",Date,"12/31/2002"))
Output:
1/14/2002 11 351 505440
Esempio 2
Attenzione: nel codice seguente, date1>date2:
document.write(Date & "<br />") document.write(DateDiff("d","12/31/2002",Date))
Output:
1/14/2002 -351
Esempio 3
Quante settimane (inizia lunedì), restano tra la data odierna e il 10/10/2002 document.write(Date & "<br />") document.write(DateDiff("w",Date,"10/10/2002",vbMonday))
Output:
1/14/2002 38