[SalesForce] Calculating the last day of next month using Ampscript

I'm looking to populate the last day of the following month for a promotion that we are running in an email. So, regardless of the current date, the promo will always end the last day of the following month. (for example, if today were June 2nd, the day I'm looking to pull in is July 31st. Same if today were June 29th, I'd still want to pull in July 31st).

Is there a simple way to do this in Ampscript?

Best Answer

You could do something like this:

%%[

set @twoMonthsDate = dateadd(now(), 2, "M")
set @month = DatePart(@twoMonthsDate, "M")
set @year = DatePart(@twoMonthsDate, "Y")
set @firstOfTwoMonthsDate = dateparse(concat(@month,"/01/",@year))
set @lastDayNextMonth = dateadd(@firstOfTwoMonthsDate,-1,"D")

]%%
<br>now(): %%=now()=%%
<br>@twoMonthsDate: %%=v(@twoMonthsDate)=%%
<br>@month: %%=v(@month)=%%
<br>@year: %%=v(@year)=%%
<br>@firstOfTwoMonthsDate: %%=v(@firstOfTwoMonthsDate)=%%
<br>@lastDayNextMonth: %%=v(@lastDayNextMonth)=%%

Output

now(): 5/31/2016 9:05:14 AM 
@twoMonthsDate: 7/31/2016 9:05:14 AM 
@month: 07 
@year: 2016 
@firstOfTwoMonthsDate: 7/1/2016 12:00:00 AM 
@lastDayNextMonth: 6/30/2016 12:00:00 AM 
Related Topic