ASP tutorials: DateDiff functions and DATEADD functions

Source: Internet
Author: User
Tags add date constant expression functions numeric string time interval

1. Date Added

DATEADD function
Returns the date that the specified time interval has been added.
DATEADD (interval, number, date)

The syntax for the DATEADD function has the following parameters
(1) Interval required option. A string expression that represents the time interval to add. For numeric values, see the "Settings" section.
(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).
(3) The date required option. Variant or text to be added to the interval representation date.

Description
You can add or subtract a specified time interval from a date by using the DATEADD function. 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, not February 31, 95. 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 is generated.
If number is not a Long value, it is rounded to the nearest integer before being evaluated.

2. Date Subtraction
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:
(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. An expression of a date. Two dates used for the calculation.
(3) FirstDayOfWeek can be selected. 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 can be selected. 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.

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.

Related Article

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.