DateDiff functions and DATEADD functions

Source: Internet
Author: User
Tags constant date1 numeric time interval

1. Date add

DateAdd function
Returns the date when the specified time interval has been added. The syntax for the
DateAdd (interval, number, date)

DateAdd function has the following parameters
(1) Interval the required option. A string expression that represents the time interval to add. For numeric values, see the "Settings" section. The
(2) number must be selected. Numeric expression that represents the number of time intervals to add. Numeric expressions can be positive (get future dates) or negative numbers (get past dates). The
(3) Date required option. Variant or text to be added to the interval representation date. The

Description
Use the DATEADD function to add or subtract a specified time interval from a date. For example, you can use DATEADD to calculate the date 30 days from today or 45 minutes from now. To add a time interval of "Day" to date, you can use the number of days per year (y), day (d), or number of days of the Week (W). The

DateAdd function does not return an invalid date. The following example adds one months to January 31, 95:

Newdate = DateAdd ("M", 1, "31-jan-95")
in this example, DATEADD returns February 28, 95 instead of 95 2 Month 31st. If date is January 31, 96, it returns February 29, 96, because 1996 is a leap year.

If the calculated date is before 100 A.D., an error occurs.
If number is not a Long value, it is rounded to the nearest integer before the calculation.

2. Date subtraction
DateDiff function
Returns the time interval between two dates.
DateDiff (interval, date1, date2 [, firstdayofweek[, FirstWeekOfYear]])

The syntax of the DateDiff function has the following parameters:
(1) Interval required option. A string expression that is used to calculate the time interval between Date1 and Date2. For numeric values, see the "Settings" section.
(2) date1, date2 required option. DayThe expression of the term. Two dates used for the calculation.
(3) FirstDayOfWeek optional. Specifies the constant for the first day of the week. If not specified, the default is Sunday. For numeric values, see the "Settings" section.
(4) FirstWeekOfYear optional. 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. The

describes the
DateDiff function 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 the day 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 December 31 and the January 1 of the following year, although the actual difference is only one day, DateDiff returns 1 to indicate a difference of a year.

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.