The use of DateDiff

Source: Internet
Author: User
Tags date constant contains date1 expression time interval
DateDiff function


Returns a Variant (long) that represents the number of time intervals between two specified dates.

Grammar

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

The following named arguments are in the DateDiff function syntax:

Partial description
Interval necessary. A string expression that represents the interval used to calculate the time difference between Date1 and Date2
Date1-date2 necessary; Variant (Date). The two dates to be used in the calculation.
FirstDayOfWeek Optional. Specifies the constant for the first day of the one week. If not specified, Sunday is the first day.
FirstWeekOfYear Optional. A constant that specifies the first week of the year. If not specified, the week that contains January 1 is the first week.


Set up

The interval parameter is set to the following values:

Set description
YYYY years
Q season
M month
Y number of days in a year
D-Day
W Number of days in a week
WW Week
H time
N Minutes
s seconds


The FirstDayOfWeek parameter is set to the following values:

Constant numerical description
Vbusesystem 0 uses the NLS API settings.
Vbsunday 1 week day (default)
Vbmonday 2 weeks A
Vbtuesday 3 week Two
Vbwednesday 4 week Three
Vbthursday 5 week Four
Vbfriday 6 Friday
Vbsaturday 7 Saturday

Constant numerical description
Vbusesystem 0 is set with the NLS API.
VbFirstJan1 1 starts with the week that contains January 1 (the default value).
vbFirstFourDays 2 begins the week of the first half of its week in the new year.
Vbfirstfullweek 3 begins with the first straddling week.


Description

The DateDiff function can be used to determine the number of time intervals specified between two dates. For example, you can use DateDiff to calculate the number of days between two dates, or to calculate how many weeks from today to the end of the year.

To calculate the number of days between Date1 and Date2, you can use the number of days of the year (Y) or day (d). When interval is a "day of the Week" (W), DateDiff returns the number of weeks for the two-day period. If Date1 is Monday, DateDiff calculates the number of Monday to Date2. This number contains date2 but does not contain date1. However, if interval is "Week" (WW), the DateDiff function returns the number of calendar weeks for the two-day period. Calculated from the number of Sunday between Date1 and Date2. If the date2 happens to be Sunday, the date2 will also be added to the DateDiff's count result, but it will not be counted whether date1 is Sunday or not.

If Date1 comes later than Date2, the return value of the DateDiff function is negative.

The FirstDayOfWeek parameter affects the results of using the time interval symbol "W" or "WW" calculations.

If Date1 or date2 is a date literal, the specified year becomes a fixed portion of that date. However, if date1 or date2 are enclosed in double quotes ("") and the year is slightly omitted, the current year is inserted into the code each time the expression date1 or date2 is evaluated. This allows you to write program code that applies to different years.

In calculating the year difference for January 1 of December 31 and the following year, DATEDIFF returns 1 to indicate a difference of one year, although in fact it is only a day difference.



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.