3

I'm trying to create dynamic content that varies depending on what week of the month it is, i.e.

IF @Week == "first friday" THEN

...

ELSEIF @Week =="second friday" THEN

Is this possible with AMPScript? You would also need to have a fallback for months with 5 weeks.

2 Answers 2

4

It is a bit 'wonky', but this should do it (and it includes the possibility for a fifth Friday):

 %%[

    SET @SubDay = SUBTRACT(DatePart(Now(), 'Day'), 1)

    SET @Negative = SUBTRACT(@SubDay, ADD(@SubDay,@SubDay))

    SET @FirstofMonth = DATEADD(NOW(), @Negative, "D")

    SET @DayofFirst = FORMATDATE(@FirstofMonth, "DDDD")

    IF @DayofFirst == "Sun" THEN

        SET @FirstFriday = DateADD(@FirstofMonth,5, "D")

    ELSEIF @DayofFirst == "Mon" THEN

        SET @FirstFriday = DateADD(@FirstofMonth,4, "D")

    ELSEIF @DayofFirst == "Tue" THEN

        SET @FirstFriday = DateADD(@FirstofMonth,3, "D")

    ELSEIF @DayofFirst == "Wed" THEN

        SET @FirstFriday = DateADD(@FirstofMonth,2, "D")

    ELSEIF @DayofFirst == "Thu" THEN

        SET @FirstFriday = DateADD(@FirstofMonth,1, "D")

    ELSEIF @DayofFirst == "Fri" THEN

        SET @FirstFriday = @FirstofMonth

    ELSEIF @DayofFirst == "Sat" THEN

        SET @FirstFriday = DateADD(@FirstofMonth,6, "D")

    ENDIF

SET @SecondFriday = DateADD(@FirstofMonth,7, "D")
SET @ThirdFriday = DateADD(@FirstofMonth,14, "D")
SET @FourthFriday = DateADD(@FirstofMonth,21, "D")
SET @FifthFriday = DateADD(@FirstofMonth,28, "D")

IF @FirstFriday > NOW() THEN

    /* First Friday Content */

ELSEIF @SecondFriday > NOW() AND NOW() > @FirstFriday THEN

    /* Second Friday Content */

ELSEIF @ThirdFriday > NOW() AND NOW() > @SecondFriday THEN

    /* Third Friday Content */

ELSEIF @FourthFriday > NOW() AND NOW() > @ThirdFriday THEN

    /* Fourth Friday Content */

ELSEIF DatePart(@FifthFriday, 'Month') == DatePart(@FourthFriday, 'Month') AND @FifthFriday > NOW() AND NOW() > @FourthFriday THEN

    /* Fifth Friday Content */

ENDIF

    ]%%

This is the First Friday: %%=v(@FirstFriday)=%%<br>
This is the Second Friday: %%=v(@SecondFriday)=%%<br>
This is the Third Friday: %%=v(@ThirdFriday)=%%<br>
This is the Fourth Friday: %%=v(@FourthFriday)=%%<br>
%%[IF DatePart(@FifthFriday, 'Month') == DatePart(@FourthFriday, 'Month') THEN]%%
    This is the Fifth Friday: %%=v(@FifthFriday)=%%<br>
%%[ENDIF]%%

OUTPUT: (on 9/28/17)

This is the First Friday: 9/1/2017 7:26:50 AM
This is the Second Friday: 9/8/2017 7:26:50 AM
This is the Third Friday: 9/15/2017 7:26:50 AM
This is the Fourth Friday: 9/22/2017 7:26:50 AM
This is the Fifth Friday: 9/29/2017 7:26:50 AM
1
  • Works perfectly! Thanks for the taking the time to think it out.
    – cmccarra
    Commented Sep 28, 2017 at 14:56
2

Fascinating question!

Here's how I'd approach calculating those dates (based on info in this answer on StackOverflow):

%%[/* Modify to view AMPScript <div style="display:none"> */

/* adapted from: https://stackoverflow.com/a/5422046/812377 */

set @today = now(1)
set @today = "2017-10-15"
set @monthOfThisMonth = DatePart(@today, "M")
set @yearOfThisMonth = DatePart(@today, "Y")
set @firstDayOfMonth = dateparse(concat(@monthOfThisMonth,"/01/",@yearOfThisMonth))

output(concat("<br>today: ",@today))
output(concat("<br>firstDayOfMonth: ",@firstDayOfMonth))

set @dayOfWeek = Format(@today,"ddd")
output(concat("<br>dayOfWeek: ",@dayOfWeek))

var @dayOfWeekNum

if @dayOfWeek == "Sun" then set @dayOfWeekNum = 1
elseif @dayOfWeek == "Mon" then set @dayOfWeekNum = 2
elseif @dayOfWeek == "Tue" then set @dayOfWeekNum = 3
elseif @dayOfWeek == "Wed" then set @dayOfWeekNum = 4
elseif @dayOfWeek == "Thu" then set @dayOfWeekNum = 5
elseif @dayOfWeek == "Fri" then set @dayOfWeekNum = 6
else set @dayOfWeekNum = 7
endif

output(concat("<br>dayOfWeekNum: ",@dayOfWeekNum))

set @dayOfWeekFriday = "Fri"
output(concat("<br>dayOfWeekFriday: ",@dayOfWeekFriday))

set @dayOfWeekNumFriday = 6
output(concat("<br>dayOfWeekNumFriday: ",@dayOfWeekNumFriday))

set @offset0 = subtract(@dayOfWeekNumFriday, @dayOfWeekNum)
output(concat("<br>offset0: ",@offset0))

set @offset1 = iif(@offset0 < 0, add(@offset0,7), @offset0)
output(concat("<br>offset1: ",@offset1))

set @dayOfWeekFirstDayOfMonth = Format(@firstDayOfMonth,"ddd")
output(concat("<br>dayOfWeekFirstDayOfMonth: ",@dayOfWeekFirstDayOfMonth))

if @dayOfWeekFirstDayOfMonth == @dayOfWeekFriday then
  set @offset = 0
else
  set @offset = @offset1
endif
output(concat("<br>offset: ",@offset))

set @firstFridayOfTheMonth = dateadd(@firstDayOfMonth,@offset,"D")
output(concat("<br>firstFridayOfTheMonth: ",@firstFridayOfTheMonth))

set @secondFridayOfTheMonth = dateadd(@firstDayOfMonth,add(@offset,7),"D")
output(concat("<br>secondFridayOfTheMonth: ",@secondFridayOfTheMonth))

set @thirdFridayOfTheMonth = dateadd(@firstDayOfMonth,add(@offset,14),"D")
output(concat("<br>thirdFridayOfTheMonth: ",@thirdFridayOfTheMonth))

set @fourthFridayOfTheMonth = dateadd(@firstDayOfMonth,add(@offset,21),"D")

if DatePart(@today, "M") == DatePart(@fourthFridayOfTheMonth, "M") then
  output(concat("<br>fourthFridayOfTheMonth: ",@fourthFridayOfTheMonth))
else
  output(concat("<br>fourthFridayOfTheMonth: n/a"))
endif

set @fifthFridayOfTheMonth = dateadd(@firstDayOfMonth,add(@offset,28),"D")

if @monthOfThisMonth == DatePart(@fifthFridayOfTheMonth, "M") then
  output(concat("<br>fifthFridayOfTheMonth: ",@fifthFridayOfTheMonth))
else
  output(concat("<br>fifthFridayOfTheMonth: n/a"))
endif

/*</div>*/
]%%

Output

today: 2017-10-15
firstDayOfMonth: 10/1/2017 12:00:00 AM
dayOfWeek: Sun
dayOfWeekNum: 1
dayOfWeekFriday: Fri
dayOfWeekNumFriday: 6
offset0: 5
offset1: 5
dayOfWeekFirstDayOfMonth: Sun
offset: 5
firstFridayOfTheMonth: 10/6/2017 12:00:00 AM
secondFridayOfTheMonth: 10/13/2017 12:00:00 AM
thirdFridayOfTheMonth: 10/20/2017 12:00:00 AM
fourthFridayOfTheMonth: 10/27/2017 12:00:00 AM
fifthFridayOfTheMonth: n/a 
3
  • Thanks! Both solid answers. Didn't think it would be elegant but works a charm.
    – cmccarra
    Commented Sep 28, 2017 at 14:59
  • Questions like yours are why I like SFSE! Commented Sep 28, 2017 at 15:00
  • Just to add why I wanted to do this: I wanted to create an onboarding automated journey that runs every week, inside the journey is an email with this ampscript. Depending on the week of the month the ampscript points to different content blocks/offers keeping the content fresh.
    – cmccarra
    Commented Sep 28, 2017 at 15:05

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .