Dan231 wrote:
I'm looking to get the last day of the month, 3 months ahead.
So if today is 10/26/16, I need the date of 1/31/17
I cannot figure out the VB script examples
Hi Dan,
I think the below should do what you want. It returns the date in format YYYY-MM-DD of the last day of three months from now. I've used this format as it's what I had to hand, I've explained what I've done though, so hopefully you can work out how to adjust the format yourself, the below notes should also help explain how my examples above work.
%{year(DateAdd("m", 3, date))}%-%{Right("0" + cstr(month(DateAdd("m", 3, date))), 2)}%-%{Right("0" + cstr(day(DateAdd("d", -1, DateAdd("m", 4,dateserial(year(date),month(date),1))))), 2)}%
We get this by first finding the year in three months:
year(DateAdd("m", 3, date))
year() returns only the year from the date passed to it, DateAdd() is used to add 3 (parameter 2) months (parameter 1) to the current date (parameter 3)
We then do the same for the month, but this time it's a bit more confusing as we want the leading zero.
Right("0" + cstr(month(DateAdd("m", 3, date))), 2)
We can see the same basics in the middle, of selecting only the month, from the current date plus 3 months, but this is also wrapped in a function that will add 0 to the beginning of the string returned by month, but truncated to the 2 right-most characters. This means if the month returns '11', we add 0 to make '011' and then select the two right-most characters leaving just '11', however if the month is 3, we add 0 to make '03', and then select the two right most characters, leaving '03'.
The day is yet more complicated, as we have elements of both of the above:
Right("0" + cstr(day(DateAdd("d", -1, DateAdd("m", 4,dateserial(year(date),month(date),1))))), 2)
We have the same functionality to add the leading zero to the day that is returned, but finding the last day of the month is slightly tricky. From the middle out, we start by making a date equal to the first of this month:
dateserial(year(date),month(date),1)
dateSerial() creates a date given 3 parameters, for year, month and day. dateSerial(Year, Month, Day)
For the year and month, we can just use the values from the current date, the day we hardcode to 1.
Once we have produced this date, we add 4 months, to give us the 1st of the month after you want the last day of.
DateAdd("m", 4,dateserial(year(date),month(date),1))
We can then subtract one day, using dateAdd, to give us the last day of the month before.
DateAdd("d", -1, DateAdd("m", 4,dateserial(year(date),month(date),1)))