<h1>DateDiff 函数</h1>
<p>返回两个日期之间的时间间隔。 </p>
<p><code><strong>DateDiff(</strong>interval, date1, date2 [,firstdayofweek[, firstweekofyear]]<strong>)</strong></code></p>
<p><strong>DateDiff</strong> 函数的语法有以下参数:</p>
<h4>参数</h4>
<p class="dt"><em>interval</em></p>
<p class="indent">必选项。<a href="http://book.chinaz.com/VBScript/vbscript_1/html/vtglossary.htm#defstringexpressionvbs">String expression</a> 表示用于计算 <strong>date1</strong> 和 <strong>date2</strong> 之间的时间间隔。有关数值,请参阅“设置”部分。</p>
<p class="dt"><em>date1, date2</em></p>
<p class="indent">必选项。日期表达式。用于计算的两个日期。</p>
<p class="dt"><em>Firstdayofweek</em></p>
<p class="indent">可选项。指定星期中第一天的常数。如果没有指定,则默认为星期日。有关数值,请参阅“设置”部分。</p>
<p class="dt"><em>Firstweekofyear</em></p>
<p class="indent">可选项。指定一年中第一周的常数。如果没有指定,则默认为 1 月 1 日所在的星期。有关数值,请参阅“设置”部分。</p>
<h4>设置</h4>
<p><strong>interval</strong> 参数可以有以下值:</p>
<table rules="all" frame="box" cols="2" cellspacing="0" border="1"><tbody>
<tr valign="top">
<th width="15%">设置</th>
<th width="85%">描述</th>
</tr>
<tr valign="top">
<td width="15%">yyyy</td>
<td width="85%">年</td>
</tr>
<tr valign="top">
<td width="15%">q</td>
<td width="85%">季度</td>
</tr>
<tr valign="top">
<td width="15%">m</td>
<td width="85%">月</td>
</tr>
<tr valign="top">
<td width="15%">y</td>
<td width="85%">一年的日数</td>
</tr>
<tr valign="top">
<td width="15%">d</td>
<td width="85%">日</td>
</tr>
<tr valign="top">
<td width="15%">w</td>
<td width="85%">一周的日数</td>
</tr>
<tr valign="top">
<td width="15%">ww</td>
<td width="85%">周</td>
</tr>
<tr valign="top">
<td width="15%">h</td>
<td width="85%">小时</td>
</tr>
<tr valign="top">
<td width="15%">n</td>
<td width="85%">分钟</td>
</tr>
<tr valign="top">
<td width="15%">s</td>
<td width="85%">秒</td>
</tr>
</tbody></table>
<br><p>firstdayofweek 参数可以有以下值:</p>
<table rules="all" frame="box" cols="3" cellspacing="0" border="1"><tbody>
<tr valign="top">
<th width="19%">常数</th>
<th width="9%">值</th>
<th width="72%">描述</th>
</tr>
<tr valign="top">
<td width="19%">vbUseSystem</td>
<td width="9%">0</td>
<td width="72%">使用区域语言支持 (NLS) API 设置。</td>
</tr>
<tr valign="top">
<td width="19%">vbSunday</td>
<td width="9%">1</td>
<td width="72%">星期日(默认)</td>
</tr>
<tr valign="top">
<td width="19%">vbMonday</td>
<td width="9%">2</td>
<td width="72%">星期一</td>
</tr>
<tr valign="top">
<td width="19%">vbTuesday</td>
<td width="9%">3</td>
<td width="72%">星期二</td>
</tr>
<tr valign="top">
<td width="19%">vbWednesday</td>
<td width="9%">4</td>
<td width="72%">星期三</td>
</tr>
<tr valign="top">
<td width="19%">vbThursday</td>
<td width="9%">5</td>
<td width="72%">星期四</td>
</tr>
<tr valign="top">
<td width="19%">vbFriday</td>
<td width="9%">6</td>
<td width="72%">星期五</td>
</tr>
<tr valign="top">
<td width="19%">vbSaturday</td>
<td width="9%">7</td>
<td width="72%">星期六</td>
</tr>
</tbody></table>
<br><p>firstweekofyear 参数可以有以下值:</p>
<table rules="all" frame="box" cols="3" cellspacing="0" border="1"><tbody>
<tr valign="top">
<th width="22%">常数</th>
<th width="8%">值</th>
<th width="70%">描述</th>
</tr>
<tr valign="top">
<td width="22%">vbUseSystem</td>
<td width="8%">0</td>
<td width="70%">使用区域语言支持 (NLS) API 设置。</td>
</tr>
<tr valign="top">
<td width="22%">vbFirstJan1</td>
<td width="8%">1</td>
<td width="70%">由 1 月 1 日所在的星期开始(默认)。</td>
</tr>
<tr valign="top">
<td width="22%">vbFirstFourDays</td>
<td width="8%">2</td>
<td width="70%">由在新年中至少有四天的第一周开始。</td>
</tr>
<tr valign="top">
<td width="22%">vbFirstFullWeek</td>
<td width="8%">3</td>
<td width="70%">由在新的一年中第一个完整的周开始。</td>
</tr>
</tbody></table>
<br><h4>说明</h4>
<p><strong>DateDiff</strong> 函数用于判断在两个日期之间存在的指定时间间隔的数目。例如可以使用 <strong>DateDiff</strong> 计算两个日期相差的天数,或者当天到当年最后一天之间的星期数。</p>
<p>要计算 <strong>date1</strong> 和 <strong>date2</strong> 相差的天数,可以使用“一年的日数”(“y”)或“日”(“d”)。当 <strong>interval </strong>为“一周的日数”(“w”)时,<strong>DateDiff</strong> 返回两个日期之间的星期数。如果 <strong>date1</strong> 是星期一,则 <strong>DateDiff</strong> 计算到 <strong>date2</strong> 之前星期一的数目。此结果包含 <strong>date2</strong> 而不包含 <strong>date1</strong>。如果 <strong>interval</strong> 是“周”(“ww”),则 <strong>DateDiff</strong> 函数返回日历表中两个日期之间的星期数。函数计算 <strong>date1</strong> 和 <strong>date2</strong> 之间星期日的数目。如果 <strong>date2</strong> 是星期日,<strong>DateDiff</strong> 将计算 <strong>date2</strong>,但即使 <strong>date1</strong> 是星期日,也不会计算 <strong>date1</strong>。</p>
<p>如果 <strong>date1</strong> 晚于 <strong>date2</strong>,则 <strong>DateDiff</strong> 函数返回负数。</p>
<p><strong>firstdayofweek</strong> 参数会对使用“w”和“ww”间隔符号的计算产生影响。</p>
<p>如果 <strong>date1</strong> 或 <strong>date2</strong> 是日期文字,则指定的年度会成为日期的固定部分。但是如果 <strong>date1</strong> 或 <strong>date2</strong> 被包括在引号 (" ") 中并且省略年份,则在代码中每次计算 <strong>date1</strong> 或 <strong>date2</strong> 表达式时,将插入当前年份。这样就可以编写适用于不同年份的程序代码。</p>
<p>在 <strong>interval </strong>为“年”(“yyyy”)时,比较 12 月 31 日和来年的 1 月 1 日,虽然实际上只相差一天,<strong>DateDiff</strong> 返回 1 表示相差一个年份。</p>
<p>下面的示例利用 <strong>DateDiff</strong> 函数显示今天与给定日期之间间隔天数: </p>
<pre><code>Function DiffADate(theDate)<br> DiffADate = "</code>从当天开始的天数<code>:" & <span class="cfe"><strong>DateDiff(</strong></span>"d"<span class="cfe"><strong>,</strong></span> Now<span class="cfe"><strong>,</strong></span> theDate<span class="cfe"><strong>)</strong></span></code><br><code>End Function</code></pre>
<p>返回两个日期之间的时间间隔。 </p>
<p><code><strong>DateDiff(</strong>interval, date1, date2 [,firstdayofweek[, firstweekofyear]]<strong>)</strong></code></p>
<p><strong>DateDiff</strong> 函数的语法有以下参数:</p>
<h4>参数</h4>
<p class="dt"><em>interval</em></p>
<p class="indent">必选项。<a href="http://book.chinaz.com/VBScript/vbscript_1/html/vtglossary.htm#defstringexpressionvbs">String expression</a> 表示用于计算 <strong>date1</strong> 和 <strong>date2</strong> 之间的时间间隔。有关数值,请参阅“设置”部分。</p>
<p class="dt"><em>date1, date2</em></p>
<p class="indent">必选项。日期表达式。用于计算的两个日期。</p>
<p class="dt"><em>Firstdayofweek</em></p>
<p class="indent">可选项。指定星期中第一天的常数。如果没有指定,则默认为星期日。有关数值,请参阅“设置”部分。</p>
<p class="dt"><em>Firstweekofyear</em></p>
<p class="indent">可选项。指定一年中第一周的常数。如果没有指定,则默认为 1 月 1 日所在的星期。有关数值,请参阅“设置”部分。</p>
<h4>设置</h4>
<p><strong>interval</strong> 参数可以有以下值:</p>
<table rules="all" frame="box" cols="2" cellspacing="0" border="1"><tbody>
<tr valign="top">
<th width="15%">设置</th>
<th width="85%">描述</th>
</tr>
<tr valign="top">
<td width="15%">yyyy</td>
<td width="85%">年</td>
</tr>
<tr valign="top">
<td width="15%">q</td>
<td width="85%">季度</td>
</tr>
<tr valign="top">
<td width="15%">m</td>
<td width="85%">月</td>
</tr>
<tr valign="top">
<td width="15%">y</td>
<td width="85%">一年的日数</td>
</tr>
<tr valign="top">
<td width="15%">d</td>
<td width="85%">日</td>
</tr>
<tr valign="top">
<td width="15%">w</td>
<td width="85%">一周的日数</td>
</tr>
<tr valign="top">
<td width="15%">ww</td>
<td width="85%">周</td>
</tr>
<tr valign="top">
<td width="15%">h</td>
<td width="85%">小时</td>
</tr>
<tr valign="top">
<td width="15%">n</td>
<td width="85%">分钟</td>
</tr>
<tr valign="top">
<td width="15%">s</td>
<td width="85%">秒</td>
</tr>
</tbody></table>
<br><p>firstdayofweek 参数可以有以下值:</p>
<table rules="all" frame="box" cols="3" cellspacing="0" border="1"><tbody>
<tr valign="top">
<th width="19%">常数</th>
<th width="9%">值</th>
<th width="72%">描述</th>
</tr>
<tr valign="top">
<td width="19%">vbUseSystem</td>
<td width="9%">0</td>
<td width="72%">使用区域语言支持 (NLS) API 设置。</td>
</tr>
<tr valign="top">
<td width="19%">vbSunday</td>
<td width="9%">1</td>
<td width="72%">星期日(默认)</td>
</tr>
<tr valign="top">
<td width="19%">vbMonday</td>
<td width="9%">2</td>
<td width="72%">星期一</td>
</tr>
<tr valign="top">
<td width="19%">vbTuesday</td>
<td width="9%">3</td>
<td width="72%">星期二</td>
</tr>
<tr valign="top">
<td width="19%">vbWednesday</td>
<td width="9%">4</td>
<td width="72%">星期三</td>
</tr>
<tr valign="top">
<td width="19%">vbThursday</td>
<td width="9%">5</td>
<td width="72%">星期四</td>
</tr>
<tr valign="top">
<td width="19%">vbFriday</td>
<td width="9%">6</td>
<td width="72%">星期五</td>
</tr>
<tr valign="top">
<td width="19%">vbSaturday</td>
<td width="9%">7</td>
<td width="72%">星期六</td>
</tr>
</tbody></table>
<br><p>firstweekofyear 参数可以有以下值:</p>
<table rules="all" frame="box" cols="3" cellspacing="0" border="1"><tbody>
<tr valign="top">
<th width="22%">常数</th>
<th width="8%">值</th>
<th width="70%">描述</th>
</tr>
<tr valign="top">
<td width="22%">vbUseSystem</td>
<td width="8%">0</td>
<td width="70%">使用区域语言支持 (NLS) API 设置。</td>
</tr>
<tr valign="top">
<td width="22%">vbFirstJan1</td>
<td width="8%">1</td>
<td width="70%">由 1 月 1 日所在的星期开始(默认)。</td>
</tr>
<tr valign="top">
<td width="22%">vbFirstFourDays</td>
<td width="8%">2</td>
<td width="70%">由在新年中至少有四天的第一周开始。</td>
</tr>
<tr valign="top">
<td width="22%">vbFirstFullWeek</td>
<td width="8%">3</td>
<td width="70%">由在新的一年中第一个完整的周开始。</td>
</tr>
</tbody></table>
<br><h4>说明</h4>
<p><strong>DateDiff</strong> 函数用于判断在两个日期之间存在的指定时间间隔的数目。例如可以使用 <strong>DateDiff</strong> 计算两个日期相差的天数,或者当天到当年最后一天之间的星期数。</p>
<p>要计算 <strong>date1</strong> 和 <strong>date2</strong> 相差的天数,可以使用“一年的日数”(“y”)或“日”(“d”)。当 <strong>interval </strong>为“一周的日数”(“w”)时,<strong>DateDiff</strong> 返回两个日期之间的星期数。如果 <strong>date1</strong> 是星期一,则 <strong>DateDiff</strong> 计算到 <strong>date2</strong> 之前星期一的数目。此结果包含 <strong>date2</strong> 而不包含 <strong>date1</strong>。如果 <strong>interval</strong> 是“周”(“ww”),则 <strong>DateDiff</strong> 函数返回日历表中两个日期之间的星期数。函数计算 <strong>date1</strong> 和 <strong>date2</strong> 之间星期日的数目。如果 <strong>date2</strong> 是星期日,<strong>DateDiff</strong> 将计算 <strong>date2</strong>,但即使 <strong>date1</strong> 是星期日,也不会计算 <strong>date1</strong>。</p>
<p>如果 <strong>date1</strong> 晚于 <strong>date2</strong>,则 <strong>DateDiff</strong> 函数返回负数。</p>
<p><strong>firstdayofweek</strong> 参数会对使用“w”和“ww”间隔符号的计算产生影响。</p>
<p>如果 <strong>date1</strong> 或 <strong>date2</strong> 是日期文字,则指定的年度会成为日期的固定部分。但是如果 <strong>date1</strong> 或 <strong>date2</strong> 被包括在引号 (" ") 中并且省略年份,则在代码中每次计算 <strong>date1</strong> 或 <strong>date2</strong> 表达式时,将插入当前年份。这样就可以编写适用于不同年份的程序代码。</p>
<p>在 <strong>interval </strong>为“年”(“yyyy”)时,比较 12 月 31 日和来年的 1 月 1 日,虽然实际上只相差一天,<strong>DateDiff</strong> 返回 1 表示相差一个年份。</p>
<p>下面的示例利用 <strong>DateDiff</strong> 函数显示今天与给定日期之间间隔天数: </p>
<pre><code>Function DiffADate(theDate)<br> DiffADate = "</code>从当天开始的天数<code>:" & <span class="cfe"><strong>DateDiff(</strong></span>"d"<span class="cfe"><strong>,</strong></span> Now<span class="cfe"><strong>,</strong></span> theDate<span class="cfe"><strong>)</strong></span></code><br><code>End Function</code></pre>