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, the calculation method is date2 - date1.
If comparing years, ignore the values below the month; if comparing months, ignore the values below the day, and so on.
Note:The firstdayofweek parameter affects the calculation using the interval symbols 'w' and 'ww'.
Syntax
DateDiff(interval,date1,date2[,firstdayofweek[,firstweekofyear]])
Parameter | Description |
---|---|
interval |
Required. The unit of time interval between date1 and date2 to be calculated. The following values can be used:
|
date1,date2 | Required. Date expression. Two dates to be used in the calculation. |
firstdayofweek |
Optional. Specifies the number of days in a week, that is, the day of the week. The following values can be used:
|
firstweekofyear |
Optional. Specifies the first week of the year. The following values can be used:
|
Eksempel
Eksempel 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"))
Uddrag:
1/14/2002 11 351 505440
Eksempel 2
Bemærk i nedenstående kode, at date1>date2:
document.write(Date & "<br />") document.write(DateDiff("d","12/31/2002",Date))
Uddrag:
1/14/2002 -351
Eksempel 3
Hvor mange uger ( starter på mandag), er tilbage mellem den nuværende dato og 10/10/2002 document.write(Date & "<br />") document.write(DateDiff("w",Date,"10/10/2002",vbMonday))
Uddrag:
1/14/2002 38