VBS TUTORIAL: function-datediff function _vbs

Source: Internet
Author: User
Tags constant date1 time interval

DateDiff function

Returns the time interval between two dates.

DateDiff(interval, date1, date2 [,firstdayofweek[, firstweekofyear]])

The syntax for the DateDiff function has the following parameters:

Parameters

Interval

Required option. String expression represents the time interval between date1 and date2 calculations. For numeric values, see the "Settings" section.

Date1, Date2

Required option. An expression of a date. Two dates used for the calculation.

FirstDayOfWeek

Options available. Specifies the constant for the first day of the week. If not specified, the default is Sunday. For numeric values, see the "Settings" section.

FirstWeekOfYear

Options available. Specifies a constant for the first week of the year. If not specified, the default is the week of January 1. For numeric values, see the "Settings" section.

Set up

The interval parameter can have the following values:

ww
set description
yyyy year
q quarter
n< /td> month
y number of days in a year
d
w< /td>
week
h hours
M minutes
s sec

The FirstDayOfWeek parameter can have the following values:

Constants value Description
Vbusesystem 0 Use the regional language Support (NLS) API settings.
Vbsunday 1 Sunday (default)
Vbmonday 2 Monday
Vbtuesday 3 Tuesday
Vbwednesday 4 Wednesday
Vbthursday 5 Thursday
Vbfriday 6 Friday
Vbsaturday 7 Saturday

The FirstWeekOfYear parameter can have the following values:

Constants value Description
Vbusesystem 0 Use the regional language Support (NLS) API settings.
VbFirstJan1 1 Starting from the week of January 1 (default).
vbFirstFourDays 2 Starting from the first week at least four days in the new year.
Vbfirstfullweek 3 Start with the first full week in the new year.

Description

The DateDiff function is used to determine the number of specified time intervals that exist between two dates. For example, you can use DateDiff to calculate the number of days between two date differences, or the day to the last day of the year.

To calculate the number of days between date1 and date2 , you can use "Days of the Year" ("Y") or "Day" ("D"). When interval is the number of days of the Week ("W"),DateDiff Returns the number of weeks between two dates. If the date1 is Monday, then DateDiff calculates the number of Monday before Date2 . This result contains Date2 and does not contain date1. If interval is "Week" ("WW"), the DateDiff function returns the number of weeks between two dates in the Calendar table. function calculates the number of Sunday between date1 and date2 . If the date2 is Sunday, DateDiff will calculate the date2, but even if date1 is Sunday, it will not calculate date1.

If date1 is later than Date2, the DateDiff function returns a negative number.

The firstdayofweek parameter affects computations that use the "w" and "ww" interval symbols.

If date1 or date2 is a date literal, the specified year becomes a fixed part of the date. However, if the date1 or date2 is included in quotation marks ("") and the year is omitted, the current year is inserted each time the date1 or date2 expression is evaluated in the code. This allows you to write program code that works for different years.

When interval is "year" ("yyyy"), comparing the January 1 of December 31 and the following year, although the actual difference is only one day,DateDiff returns 1 to indicate a difference of a year.

The following example uses the DateDiff function to display the number of days between today and a given date:

  Function diffadate (thedate) diffadate = "Number of days from day :" &   DateDiff ( "D"  ,  now ,  thedate ) "  "     end Function  

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.