1

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?

3

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 
1
  • 1
    This is exactly what I was looking for, thank you!
    – Hannah
    May 31 '16 at 15:15
3

Just one additional thing to Adam Spriggs answer which might be a very valuable information. If your marketing cloud Date Format is not set on English the solution of Adam will NOT work inside email previews but in send emails and in cloudpages, because the DateParse function will be handled differently.

Example for German Date Format on the 26th February 2020, with the code on top inside an email PREVIEW:

now(): 2/26/2020 8:58:49 AM
@twoMonthsDate: 4/26/2020 8:58:49 AM
@month: 04
@year: 2020
@firstOfTwoMonthsDate: 1/4/2020 12:00:00 AM
@lastDayNextMonth: 1/3/2020 12:00:00 AM

Example for German Date Format on the 26th February 2020, with the code on top inside a Cloudpage:

now(): 2/26/2020 9:15:31 AM
@twoMonthsDate: 4/26/2020 9:15:31 AM
@month: 04
@year: 2020
@firstOfTwoMonthsDate: 4/1/2020 12:00:00 AM
@lastDayNextMonth: 3/31/2020 12:00:00 AM

When you change the DateParse to dateparse(concat("01.",@month,".",@year)) in the Email PREVIEW the results in this:

now(): 2/26/2020 9:00:12 AM
@twoMonthsDate: 4/26/2020 9:00:12 AM
@month: 04
@year: 2020
@firstOfTwoMonthsDate: 4/1/2020 12:00:00 AM
@lastDayNextMonth: 3/31/2020 12:00:00 AM

But in the inbox the email looks like this:

@twoMonthsDate: 4/26/2020 8:58:49 AM
@month: 04
@year: 2020
@firstOfTwoMonthsDate: 1/4/2020 12:00:00 AM
@lastDayNextMonth: 1/3/2020 12:00:00 AM

I was actually unaware that the Date Format of the Marketing Cloud Instance will influence the AMPScript functions in that matter, but only in emails-previews but not on cloudpages or on actual email sends(WTF?!!!!!!!!!).

Holy shit what is this?

1
  • Johannes thanks for explaining the quirks involved in AMPscript's date functions and format behaviours. Mar 7 '20 at 10:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.