VBScript DateDiff function
Definition and usage
The DateDiff function can return the number of intervals between two dates.
The DateDiff function is used to calculate the difference between two date-time values, the calculation method is date2 - date1.
When comparing years, regardless of the value below the month, when comparing months, regardless of the value 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 between date1 and date2 to be used in the calculation. The following values can be used:
|
date1,date2 | Required. Date expression. Two dates to be used in the calculation. |
firstdayofweek |
Optional. Defines the day of the week, that is, 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:
|
Παράδειγμα
Παράδειγμα 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"))
Έξοδος:
1/14/2002 11 351 505440
Παράδειγμα 2
Παρακαλώ σημειώστε ότι στο παρακάτω κώδικα, date1>date2:
document.write(Date & "<br />") document.write(DateDiff("d","12/31/2002",Date))
Έξοδος:
1/14/2002 -351
Παράδειγμα 3
Πόσα εβδομάδες (αρχή την Δευτέρα), Υπάρχουν document.write(Date & "<br />") document.write(DateDiff("w",Date,"10/10/2002",vbMonday))
Έξοδος:
1/14/2002 38