"VBA Research" uses the DATEADD function to take a date from the previous month or the same year

Source: Internet
Author: User

Iamlaosong

The DateAdd function returns a date that is added to a time interval. This function can be used to calculate many of the dates we need, such as last month's year-earlier date.

Grammar

DATEADD (interval, number, date)

The following named parameters are available in the DATEADD function syntax:

Interval necessary. The string expression is the time interval to be added. It has a lot of set values, such as "M" for the month, "D" for the day, "yyyy" for the year and so on.

Number is necessary. A numeric expression that is the number of time intervals to add. Its value can be a positive number (get a future date), or it can be negative (get past date).

Date necessary. The text that represents the date.

Here's an example of how to use this function to get the date we need:

    E_date1 = CDate ("2015-3-18")                          ' assignment    b_date1 = E_date1-day (e_date1) + 1                  ' generally takes 1st of the month method    b_date1 = DateAdd ("D", 1-day (e_date1), b_date1)     ' uses the DATEADD function to take    the month 1st y_date1 = DateAdd ("M", 1-month (b_date1), b_date1)   ' using DATEADD function Fetch January 1    e_date2 = DateAdd ("M",-1, e_date1)                   ' use DATEADD function to take the same period last month    E_date3 = DateAdd ("M", -12, E_date1)                  ' Use the DATEADD function to take the same period of the previous year    e_date3 = DATEADD ("yyyy",-1, e_date1)                ' using DATEADD function to take the same year

Note that when taking the same period of the previous month, if the current date is greater than the total number of days of the month, then the maximum number of days of the month as a date, for example, March 31 month of the same period, if the total number of days in February is 28, the date taken out is February 28 and does not produce an incorrect date.


"VBA Research" uses the DATEADD function to take a date from the previous month or the same 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.