3

In AMPScript, how can I determine the first and last days of a week based on the current date?

1

To get the MM/DD/YYYY date for beginning of week and end of week, I believe the below AMPScript should do the trick. Keep in mind on an email, this will be based on when the AMPscript was run (at send time usually) and not at live view.

 %%[

    SET @Today = FormatDate(NOW(), "dddd")

    SET @Month = FormatDate(Now(), "mm") /* or "MMMM" if you want full month name */
    SET @Year = FormatDate(Now(), "YYYY")
    SET @Day = FormatDate(Now(), "dd")

    IF @Today == "Sun" THEN

        SET @DayBegin = @Day
        SET @DayEnd = Add(@Day,6)

    ELSEIF @Today == "Mon" THEN

        SET @DayBegin = Subtract(@Day,1)
        SET @DayEnd = Add(@Day,5)

    ELSEIF @Today == "Tue" THEN

        SET @DayBegin = Subtract(@Day,2)
        SET @DayEnd = Add(@Day,4)

    ELSEIF @Today == "Wed" THEN

        SET @DayBegin = Subtract(@Day,3)
        SET @DayEnd = Add(@Day,3)

    ELSEIF @Today == "Thu" THEN

        SET @DayBegin = Subtract(@Day,4)
        SET @DayEnd = Add(@Day,2)

    ELSEIF @Today == "Fri" THEN

        SET @DayBegin = Subtract(@Day,5)
        SET @DayEnd = Add(@Day,1)

    ELSEIF @Today == "Sat" THEN

        SET @DayBegin = Subtract(@Day,6)
        SET @DayEnd = @Day

    ENDIF

        SET @BeginDate = CONCAT(@Month,CONCAT("/",CONCAT(@DayBegin,CONCAT("/", @Year))))
        SET @EndDate = CONCAT(@Month,CONCAT("/",CONCAT(@DayEnd,CONCAT("/", @Year))))

    ]%%

This is the beginning of the week: %%=v(@BeginDate)=%%<br>
This is the end of the week: %%=v(@EndDate)=%%<br>

This would output the following:

This is the beginning of the week: 2/7/2016
This is the end of the week: 2/13/2016

|improve this answer|||||

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.