dateDiff(interval,date1,date2[,firstdayofweek][ ,firstweekofyear])函数用于计算两个日期之间的时间间隔。
示例
Dim fromDate As Variant
Dim toDate As Variant
fromDate = "01-Jan-09 00:00:00"
toDate = "01-Jan-10 23:59:00"
MsgBox ("年数差异: " & DateDiff("yyyy", fromDate, toDate))
MsgBox ("季度差异: " & DateDiff("q", fromDate, toDate))
MsgBox ("月数差异: " & DateDiff("m", fromDate, toDate))
MsgBox ("天数差异: " & DateDiff("d", fromDate, toDate))
MsgBox ("小时差异: " & DateDiff("h", fromDate, toDate))
MsgBox ("分钟差异: " & DateDiff("n", fromDate, toDate))
MsgBox ("秒数差异: " & DateDiff("s", fromDate, toDate))
参数说明
-
interval:必需。表示用于计算 date1 和 date2 之间差异的时间间隔的字符串表达式1。
-
date1、date2:必需。要在计算中使用的两个日期2。
-
firstdayofweek:可选。一个指定一周的第一天的常量。如果未指定,则会假定为星期日1。
-
firstweekofyear:可选。一个指定一年的第一周的常量。如果未指定,则会假定 1 月 1 日出现的那一周为第一周2。
时间间隔设置
-
yyyy:年
-
q:季度
-
m:月
-
y:每年的某一日
-
d:天
-
w:工作日
-
ww:周
-
h:小时
-
n:分钟
-
s:秒1