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