Table of contents

DateDiff 函数DateDiff Function

office 365 dev account|上次更新日期: 2018/3/13
2 参与人员

返回一个variant 类型的值() 指定两个指定日期之间的时间间隔数。语法DateDiff (间隔、 date1 和 date2 ** [ **、 firstdayofweek [ firstweekofyear ]] )DateDiff函数语法具有下面这些命名参数Returns a Variant ( Long ) specifying the number of time intervals between two specified dates. SyntaxDateDiff( interval, date1, date2 [ , firstdayofweek [ ,firstweekofyear ]] ) The DateDiff function syntax has thesenamed arguments:

时间间隔interval必填。字符串表达式,用于计算date1date2之间差异的时间的间隔。Required. String expression that is the interval of time you use to calculate the difference between date1 and date2.
日期 1日期 2date1, date2所需; Variant 类型的值日期)。要在计算中使用的两个日期。Required; Variant ( Date ). Two dates you want to use in the calculation.
firstdayofweekfirstdayofweek可选。常量,指定一周的第一天。如果未指定,则假设为星期日。Optional. A constant that specifies the first day of the week. If not specified, Sunday is assumed.
firstweekofyearfirstweekofyear可选。一个指定一年的第一周的常量。如果未指定,则假定第一周为 1 月 1 日所在的周。Optional. A constant that specifies the first week of the year. If not specified, the first week is assumed to be the week in which January 1 occurs.

设置间隔参数具有以下设置:Settings The intervalargument has these settings:

yy一年中的一天Day of year

Firstdayofweek参数具有以下设置:The firstdayofweek argument has these settings:

vbUseSystemvbUseSystem00使用 NLS API 设置。Use the NLS API setting.
vbSundayvbSunday11周日(默认)Sunday (default)
vbUseSystemvbUseSystem00使用 NLS API 设置。Use the NLS API setting.
vbFirstJan1vbFirstJan111从 1 月 1 日所在的周开始(默认)。Start with week in which January 1 occurs (default).
vbFirstFourDaysvbFirstFourDays22从第一个至少包含新年的四天的周开始。Start with the first week that has at least four days in the new year.
vbFirstFullWeekvbFirstFullWeek33从每年的第一个完整周开始。Start with first full week of the year.

备注您可以使用DateDiff函数来确定两个日期之间存在多少指定的时间间隔。例如,您可以使用DateDiff来计算两个日期或今天到年末之间的周数之间的天数。若要计算date1date2之间的天数,可使用年份 ("y") 的某一天或者一天 ("d")。工作日 ("w")间隔时, DateDiff返回两个日期之间的周数。如果date1落在星期一, DateDiff计数的日期 2之前的星期一。该函数计算日期 2而不是日期 1。如果间隔为一周 ("ww"),但是, DateDiff函数返回两个日期之间的日历周数。该函数计算date1date2之间星期日的数目。DateDiff计算日期 2 ,如果它落在星期日;但是,不会计算date1,即使它 does 落在星期日。如果date1指以时间比date2以后, DateDiff函数返回一个负数。Firstdayofweek参数会影响使用"w"和"ww"间隔符号的计算。如果date1date2日期文字,那么指定的年将成为该日期的固定部分。但是,如果date1或_date2_括在双引号 (""),并省略了年,当前年插入到代码中计算date1或_date2_表达式每次。这样,就可以编写代码,可在其他年份。相比,12 月 31 日到次年的 1 月 1 日, DateDiff年度 ("yyyy") 返回 1,即使只是一天过去。Remarks You can use the DateDiff function to determine how many specified time intervals exist between two dates. For example, you might use DateDiff to calculate the number of days between two dates, or the number of weeks between today and the end of the year. To calculate the number of days between date1 and date2, you can use either Day of year ("y") or Day ("d"). When interval is Weekday ("w"), DateDiff returns the number of weeks between the two dates. If date1 falls on a Monday, DateDiff counts the number of Mondays until date2. It counts date2 but not date1. If interval is Week ("ww"), however, the DateDiff function returns the number of calendar weeks between the two dates. It counts the number of Sundays between date1 and date2. DateDiff counts date2 if it falls on a Sunday; but it doesn't count date1, even if it does fall on a Sunday. If date1 refers to a later point in time than date2, the DateDiff function returns a negative number. The firstdayofweek argument affects calculations that use the "w" and "ww" interval symbols. If date1 or date2 is adate literal, the specified year becomes a permanent part of that date. However, if date1 or date2 is enclosed in double quotation marks (" "), and you omit the year, the current year is inserted in your code each time the date1 or date2 expression is evaluated. This makes it possible to write code that can be used in different years. When comparing December 31 to January 1 of the immediately succeeding year, DateDiff for Year ("yyyy") returns 1 even though only a day has elapsed.

请注意 对于date1date2,如果Calendar属性设置为公历,所提供的日期必须是公历。如果日历为回历,则所提供的日期必须是回历。Note For date1 and date2, if the Calendar property setting is Gregorian, the supplied date must be Gregorian. If the calendar is Hijri, the supplied date must be Hijri.


本示例使用DateDiff函数显示在给定的日期和今天之间的天数。This example uses the DateDiff function to display the number of days between a given date and today.

Dim TheDate As Date    ' Declare variables.
Dim Msg
TheDate = InputBox("Enter a date")
Msg = "Days from today: " &; DateDiff("d", Now, TheDate)
MsgBox Msg

© 2018 Microsoft