📄 vbs93.htm
字号:
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN"><HTML><HEAD><TITLE>DateDiff Function</TITLE>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso8859-1">
<META NAME="MS.LOCALE" CONTENT="EN-US">
<META NAME="PRODUCT" CONTENT="Visual Basic Scripting Edition">
<META NAME="TECHNOLOGY" CONTENT="SCRIPTING">
<META NAME="CATEGORY" CONTENT="Language Reference">
<META NAME="Keywords" CONTENT=""><META NAME="Description" CONTENT="DateDiff Function"></HEAD>
<BODY BGCOLOR=FFFFFF LINK=#0033CC>
<!--TOOLBAR_START-->
<!--TOOLBAR_EXEMPT-->
<!--TOOLBAR_END-->
<FONT FACE="Verdana, Arial, Helvetica" SIZE=2>
<TABLE BORDER=0 CELLPADDING=0 CELLSPACING=0 WIDTH=100%><TR VALIGN=TOP><TD WIDTH=360>
<FONT SIZE=1 COLOR=#660033>Microsoft® Visual Basic® Scripting Edition</FONT><BR>
<FONT SIZE=5 COLOR=#660033><B>DateDiff Function</B></FONT>
</TD>
<TD ALIGN=RIGHT>
<FONT SIZE=2> <A HREF="vbstoc.htm">Language Reference</A> <BR>
<A HREF="vbs16.htm">Version 2</A> <P></FONT>
</TD></TR>
</TABLE>
<FONT SIZE=2><P>
<A HREF="vbs94.htm">See Also</A></FONT>
<HR noshade SIZE=1>
<H5>Description</H5>
<BLOCKQUOTE>
Returns the number of intervals between two dates.
</BLOCKQUOTE>
<H5>Syntax</H5>
<BLOCKQUOTE>
<b>DateDiff(</b><i>interval</i><B>,</B> <i>date1</i><B>, </B><i>date2</i> [<B>,</B><i>firstdayofweek</i>[<B>,</B> <i>firstweekofyear</i>]]<b>)</b><P>
The <b>DateDiff</b> function syntax has these parts:<P>
<TABLE WIDTH=87% BORDER=1 CELLPADDING=5 CELLSPACING=0>
<TR VALIGN=TOP BGCOLOR="#DDDDDD">
<TD><FONT SIZE=2><b>Part</b></FONT></TD>
<TD><FONT SIZE=2><b>Description</b></FONT></TD></TR>
<TR VALIGN=TOP>
<TD><FONT SIZE=2><i>interval</i></FONT></TD>
<TD><FONT SIZE=2>Required. String expression that is the interval you want to use to calculate the differences between <i>date1</i> and <i>date2</i>. See Settings section for values.</FONT></TD></TR>
<TR VALIGN=TOP>
<TD><FONT SIZE=2><i>date1, date2</i></FONT></TD>
<TD><FONT SIZE=2>Required. Date expressions. Two dates you want to use in the calculation.</FONT></TD></TR>
<TR VALIGN=TOP>
<TD><FONT SIZE=2><i>firstdayofweek</i></FONT></TD>
<TD><FONT SIZE=2>Optional. Constant that specifies the day of the week. If not specified, Sunday is assumed. See Settings section for values.</FONT></TD></TR>
<TR VALIGN=TOP>
<TD><FONT SIZE=2><i>firstweekofyear</i></FONT></TD>
<TD><FONT SIZE=2>Optional. 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. See Settings section for values.</FONT></TD></TR>
</TABLE>
</BLOCKQUOTE>
<H5>Settings</H5>
<BLOCKQUOTE>
The <i>interval</i> argument can have the following values:<p>
<TABLE WIDTH=87% BORDER=1 CELLPADDING=5 CELLSPACING=0>
<TR VALIGN=TOP BGCOLOR="#DDDDDD">
<TD WIDTH=15%><FONT SIZE=2><b>Setting</b></FONT></TD>
<TD><FONT SIZE=2><b>Description</b></FONT></TD></TR>
<TR VALIGN=TOP>
<TD WIDTH=15%><FONT SIZE=2><i>yyyy</i></FONT></TD>
<TD><FONT SIZE=2>Year</FONT></TD></TR>
<TR VALIGN=TOP>
<TD WIDTH=15%><FONT SIZE=2><i>q</i></FONT></TD>
<TD><FONT SIZE=2>Quarter</FONT></TD></TR>
<TR VALIGN=TOP>
<TD WIDTH=15%><FONT SIZE=2><i>m</i></FONT></TD>
<TD><FONT SIZE=2>Month</FONT></TD></TR>
<TR VALIGN=TOP>
<TD><FONT SIZE=2><i>y</i></FONT></TD>
<TD><FONT SIZE=2>Day of year</FONT></TD></TR>
<TR VALIGN=TOP>
<TD WIDTH=15%><FONT SIZE=2><i>d</i></FONT></TD>
<TD><FONT SIZE=2>Day</FONT></TD></TR>
<TR VALIGN=TOP>
<TD WIDTH=15%><FONT SIZE=2><i>w</i></FONT></TD>
<TD><FONT SIZE=2>Weekday</FONT></TD></TR>
<TR VALIGN=TOP>
<TD WIDTH=15%><FONT SIZE=2><i>ww</i></FONT></TD>
<TD><FONT SIZE=2>Week of year</FONT></TD></TR>
<TR VALIGN=TOP>
<TD WIDTH=15%><FONT SIZE=2><i>h</i></FONT></TD>
<TD><FONT SIZE=2>Hour</FONT></TD></TR>
<TR VALIGN=TOP>
<TD WIDTH=15%><FONT SIZE=2><i>n</i></FONT></TD>
<TD><FONT SIZE=2>Minute</FONT></TD></TR>
<TR VALIGN=TOP>
<TD WIDTH=15%><FONT SIZE=2><i>s</i></FONT></TD>
<TD><FONT SIZE=2>Second</FONT></TD></TR>
</TABLE><p>
The <i>firstdayofweek</i> argument can have the following values:<p>
<TABLE WIDTH=87% BORDER=1 CELLPADDING=5 CELLSPACING=0>
<TR VALIGN=TOP BGCOLOR="#DDDDDD">
<TD><FONT SIZE=2><b>Constant</b></FONT></TD>
<TD WIDTH=10%><FONT SIZE=2><b>Value</b></FONT></TD>
<TD WIDTH=75%><FONT SIZE=2><b>Description</b></FONT></TD></TR>
<TR VALIGN=TOP>
<TD><FONT SIZE=2><b>vbUseSystem</b></FONT></TD>
<TD ALIGN=CENTER WIDTH=10%><FONT SIZE=2>0</FONT></TD>
<TD WIDTH=75%><FONT SIZE=2>Use National Language Support (NLS) API setting.</FONT></TD></TR>
<TR VALIGN=TOP>
<TD><FONT SIZE=2><b>vbSunday</b></FONT></TD>
<TD ALIGN=CENTER WIDTH=10%><FONT SIZE=2>1</FONT></TD>
<TD WIDTH=75%><FONT SIZE=2>Sunday (default)</FONT></TD></TR>
<TR VALIGN=TOP>
<TD><FONT SIZE=2><b>vbMonday</b></FONT></TD>
<TD ALIGN=CENTER WIDTH=10%><FONT SIZE=2>2</FONT></TD>
<TD WIDTH=75%><FONT SIZE=2>Monday</FONT></TD></TR>
<TR VALIGN=TOP>
<TD><FONT SIZE=2><b>vbTuesday</b></FONT></TD>
<TD ALIGN=CENTER WIDTH=10%><FONT SIZE=2>3</FONT></TD>
<TD WIDTH=75%><FONT SIZE=2>Tuesday</FONT></TD></TR>
<TR VALIGN=TOP>
<TD><FONT SIZE=2><b>vbWednesday</b></FONT></TD>
<TD ALIGN=CENTER WIDTH=10%><FONT SIZE=2>4</FONT></TD>
<TD WIDTH=75%><FONT SIZE=2>Wednesday</FONT></TD></TR>
<TR VALIGN=TOP>
<TD><FONT SIZE=2><b>vbThursday</b></FONT></TD>
<TD ALIGN=CENTER WIDTH=10%><FONT SIZE=2>5</FONT></TD>
<TD WIDTH=75%><FONT SIZE=2>Thursday</FONT></TD></TR>
<TR VALIGN=TOP>
<TD><FONT SIZE=2><b>vbFriday</b></FONT></TD>
<TD ALIGN=CENTER WIDTH=10%><FONT SIZE=2>6</FONT></TD>
<TD WIDTH=87%><FONT SIZE=2>Friday</FONT></TD></TR>
<TR VALIGN=TOP>
<TD><FONT SIZE=2><b>vbSaturday</b></FONT></TD>
<TD ALIGN=CENTER WIDTH=10%><FONT SIZE=2>7</FONT></TD>
<TD WIDTH=75%><FONT SIZE=2>Saturday</FONT></TD></TR>
</TABLE>
The <i>firstweekofyear</i> argument can have the following values:<p>
<TABLE WIDTH=87% BORDER=1 CELLPADDING=5 CELLSPACING=0>
<TR VALIGN=TOP BGCOLOR="#DDDDDD">
<TD><FONT SIZE=2><b>Constant</b></FONT></TD>
<TD WIDTH=10%><FONT SIZE=2><b>Value</b></FONT></TD>
<TD WIDTH=75%><FONT SIZE=2><b>Description</b></FONT></TD></TR>
<TR VALIGN=TOP>
<TD><FONT SIZE=2><b>vbUseSystem</b></FONT></TD>
<TD ALIGN=CENTER WIDTH=10%><FONT SIZE=2>0</FONT></TD>
<TD WIDTH=85%><FONT SIZE=2>Use National Language Support (NLS) API setting.</FONT></TD></TR>
<TR VALIGN=TOP>
<TD><FONT SIZE=2><b>vbFirstJan1</b></FONT></TD>
<TD ALIGN=CENTER WIDTH=10%><FONT SIZE=2>1</FONT></TD>
<TD WIDTH=85%><FONT SIZE=2>Start with the week in which January 1 occurs (default).</FONT></TD></TR>
<TR VALIGN=TOP>
<TD><FONT SIZE=2><b>vbFirstFourDays</b></FONT></TD>
<TD ALIGN=CENTER WIDTH=10%><FONT SIZE=2>2</FONT></TD>
<TD WIDTH=85%><FONT SIZE=2>Start with the week that has at least four days in the new year.</FONT></TD></TR>
<TR VALIGN=TOP>
<TD><FONT SIZE=2><b>vbFirstFullWeek</b></FONT></TD>
<TD ALIGN=CENTER WIDTH=10%><FONT SIZE=2>3</FONT></TD>
<TD WIDTH=85%><FONT SIZE=2>Start with the first full weekof the new year.</FONT></TD></TR>
</TABLE>
</BLOCKQUOTE>
<H5>Remarks</H5>
<BLOCKQUOTE>
You can use the <b>DateDiff</b> function to determine how many specified time intervals exist between two dates. For example, you might use <b>DateDiff</b> to calculate the number of days between two dates, or the number of weeks between today and the end of the year.<p>
To calculate the number of days between <i>date1</i> and <i>date2</i>, you can use either Day of year ("y") or Day ("d"). When <i>interval </i>is Weekday ("w"), <b>DateDiff</b> returns the number of weeks between the two dates. If <i>date1</i> falls on a Monday, <b>DateDiff</b> counts the number of Mondays until <i>date2</i>. It counts <i>date2</i> but not <i>date1</i>. If <i>interval</i> is Week ("ww"), however, the <b>DateDiff</b> function returns the number of calendar weeks between the two dates. It counts the number of Sundays between <i>date1</i> and <i>date2</i>. <b>DateDiff</b> counts <i>date2</i> if it falls on a Sunday; but it doesn't count <i>date1</i>, even if it does fall on a Sunday.<p>
If <i>date1</i> refers to a later point in time than <i>date2</i>, the <b>DateDiff</b> function returns a negative number.<p>
The <i>firstdayofweek</i> argument affects calculations that use the "w" and "ww" interval symbols.<p>
If <I>date1</I> or <I>date2</I> is a date literal, the specified year becomes a permanent part of that date. However, if <I>date1</I> or <I>date2</I> is enclosed in quotation marks (" ") and you omit the year, the current year is inserted in your code each time the <I>date1</I> or <I>date2</I> expression is evaluated. This makes it possible to write code that can be used in different years.<p>
When comparing December 31 to January 1 of the immediately succeeding year, <b>DateDiff</b> for Year ("yyyy") returns 1 even though only a day has elapsed.
</BLOCKQUOTE>
<hr noshade size=1>
<p align=center><em><a href="../../common/colegal.htm">© 1997 by Microsoft Corporation. All rights reserved.</a></em></p>
</FONT></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -