0

We need to populate first Thursday of month using Ampscript in email. Ex:"Thrusday,july2,2020" But my below code is not working. Pleasesuggest what went wrong here. I am not aware of complete ampscript

    %[
        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 @Firstthursday = DateADD(@FirstofMonth,4, "D")
    
        ELSEIF @DayofFirst == "Mon" THEN
    
            SET @ Firstthursday = DateADD(@FirstofMonth,3, "D")
    
        ELSEIF @DayofFirst == "Tue" THEN
    
            SET @ Firstthursday = DateADD(@FirstofMonth,2, "D")
    
        ELSEIF @DayofFirst == "Wed" THEN
    
            SET @ Firstthursday = DateADD(@FirstofMonth,1, "D")
    
        ELSEIF @DayofFirst == "Thu" THEN
    
            SET @ Firstthursday = @FirstofMonth
    
        ELSEIF @DayofFirst == "Fri" THEN
    
            SET @ Firstthursday = DateADD(@FirstofMonth,6, "D")
    
    
        ELSEIF @DayofFirst == "Sat" THEN
    
            SET @ Firstthursday = DateADD(@FirstofMonth,5, "D")
    
      
    ENDIF
    
        ]%%

This is the First Thursday of month: %%=v(@Firstthursday)=%%<br>

Thanks.

2
  • Remove the spaces between @ and Firstthursday (just use @Firstthursday) and see if it helps?
    – zuzannamj
    Commented Jun 15, 2020 at 16:13
  • working now, thanks
    – lavanya
    Commented Jun 16, 2020 at 12:11

2 Answers 2

0

You missed a '%' symbol at the start of AMPscript, it is %% and use @Firstthrusday(without a space).

Your output will result as 6/4/2020 10:38:05 PM. You need to format date using AMPscript FormatDate functions.

Try the below code, which will give you the expected output.

%%[ 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 @Firstthursday = DateADD(@FirstofMonth,4, "D")

ELSEIF @DayofFirst == "Mon" THEN

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

ELSEIF @DayofFirst == "Tue" THEN

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

ELSEIF @DayofFirst == "Wed" THEN

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

ELSEIF @DayofFirst == "Thu" THEN

    SET @Firstthursday = @FirstofMonth

ELSEIF @DayofFirst == "Fri" THEN

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


ELSEIF @DayofFirst == "Sat" THEN

    SET @Firstthursday = DateADD(@FirstofMonth,5, "D")
ENDIF


]%%

%%=v(FormatDate(@Firstthursday,"l"))=%%
1
  • Thanks. This worked
    – lavanya
    Commented Jun 16, 2020 at 12:10
1

Here is a quick little thing I put together based off of another script I had. This is a bit more condensed and utilizes a For loop instead of a bunch of conditionals.

Below is a commented version of the script to explain what it is doing:

%%[

/* Defines current Date */
set @t = Now()

/* Defines next month (adds a month to current date) */
/* I did it this way to better account for year changes (dec to jan) */
/* and leap years, etc. */
set @nm = DateAdd(@t,1,'M')

/* Grabs the month part of the new date */
set @nmMonth = DatePart(@nm,'M')

/* Grabs the year part of the new date */
set @nmYear = DatePart(@nm,'Y')

/* Creates a new date by combining month with 01 and year via Concat */
set @fonm = DateParse(CONCAT(@nmMonth,'/01/',@nmYear))

/* Grabs the day name from first of next month date */
set @fonmDay = FormatDate(@fonm,'ddddd')

/* String created to be used as rowset to find day index to next thursday for new month */
set @dayStr = "Thursday|Friday|Saturday|Sunday|Monday|Tuesday|Wednesday"

/* Turn above string into rowset */
set @dayRS = BuildRowsetFromString(@dayStr,'|')

/* For loop to iterate through new rowset */
for @i=1 TO Rowcount(@dayRS) DO

  /* Sets row to look at based on iterative (@i) */
  set @row = Row(@dayRS,@i)

  /* Sets the field to display, in this we use ordinal as there is no column name */
  set @day = Field(@row,1)

  /* Use a conditional to check if the days match */
  if @fonmDay == @day then
    
    /* Once they match, you set the 'index' to equal 8 minus the iterative */
    /* I chose 8 so that if it is Wednesday (iterative 7) we get 1 */
    /* to add a single day, etc. */
    set @index = Subtract(8,@i)

    /* Conditional that if index is greater than 6, to set to 0 */
    /* This will account for if the first is a Thursday */
    if @index > 6 then
      set @index = 0
    endif

  endif

next @i

/* Add together the index to the first of the next month to get first thursday */
set @firstThurs = DateAdd(@fonm,@index,'D')

]%%

%%=v(@firstThurs)=%%

Below is the cleaned up version without comments:

%%[

set @t = Now()

set @nm = DateAdd(@t,1,'M')
set @nmMonth = DatePart(@nm,'M')
set @nmYear = DatePart(@nm,'Y')
set @fonm = DateParse(CONCAT(@nmMonth,'/01/',@nmYear))
set @fonmDay = FormatDate(@fonm,'ddddd')

set @dayStr = "Thursday|Friday|Saturday|Sunday|Monday|Tuesday|Wednesday"
set @dayRS = BuildRowsetFromString(@dayStr,'|')

for @i=1 TO Rowcount(@dayRS) DO

  set @row = Row(@dayRS,@i)
  set @day = Field(@row,1)
  
  if @fonmDay == @day then
    set @index = Subtract(8,@i)

    if @index > 6 then
      set @index = 0
    endif

  endif

next @i

set @firstThurs = DateAdd(@fonm,@index,'D')

]%%

%%=v(@firstThurs)=%%

To change which day name you want to grab, you would just adjust the string to begin with that day instead of Thursday and then list out the rest in order. So for instance, for the first friday, all you would have to change is the dayStr value.

From: "Thursday|Friday|Saturday|Sunday|Monday|Tuesday|Wednesday"
To: "Friday|Saturday|Sunday|Monday|Tuesday|Wednesday|Thursday"

and the script will now look for the first Friday instead.

You must log in to answer this question.

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