1

how can you get the first and last day of week. But my first day should be Monday and last day should be Sunday (I should follow the UK timezone).

Here is the code from Gortonington.

%%[

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)=%%
This is the end of the week: %%=v(@EndDate)=%%

I have a problem with this every last week of the month and also if the prev month is 30 days only I get negative figure on the first day of the next month.

  • Apologies for the incorrect code! I will adjust and correct it so that it can be referenced without issue in the future. – Gortonington Oct 4 at 13:30
  • I provided my updated code below and have updated my blog post and calling out you and Cameron for your help - (gortonington.com/first-and-last-day-of-the-week). Thanks again for pointing out the inconsistency of my original code! I hate to provide anything that is not quality, so any time you find something 'wonky' on any of my work - please reach out and let me know. – Gortonington Oct 4 at 14:46
  • It's okay. Thank you so much! – CAR Oct 4 at 17:40
3

Here is my updated code that I am going to be using to replace my incorrect code from now on. This lets you set what day you want the week to begin and then correctly calculates the dates from there.

(but please give all +1's to @CameronRobert)

%%[

SET @dayofWeekBegin = "Mon"

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

SET @daysOfWeekString = "Sun|Mon|Tue|Wed|Thu|Fri|Sat"

SET @daysOfWeekRowset = BuildRowSetFromString(@daysOfWeekString, '|')

IF @Today == @dayofWeekBegin THEN

    SET @BeginDate = FormatDate(NOW(),'mm/dd/yyyy')

ENDIF



FOR @i = 1 TO Rowcount(@daysOfWeekRowset) DO 

    SET @tempDay = Field(Row(@daysOfWeekRowset,@i),1)

    IF @dayOfWeekBegin == @tempDay THEN

        set @daySub = @i

    ENDIF

NEXT @i


FOR @a = 1 TO Rowcount(@daysOfWeekRowset) DO 

    SET @tempDay = Field(Row(@daysOfWeekRowset,@a),1)

    IF @Today == @tempDay THEN

        set @dateOffset = SUBTRACT(@a, @daySub)

    ENDIF

NEXT @a


IF @dateOffset != 0 THEN

    SET @dateOffsetBegin = SUBTRACT(@dateOffset, ADD(@dateOffset,@dateOffset))
    SET @dateOffsetEnd = SUBTRACT(6,@dateOffset)

ENDIF

IF EMPTY(@BeginDate) THEN

    SET @BeginDate = FormatDate(DateAdd(Now(),@dateOffsetBegin,'D'),'mm/dd/yyyy')

ENDIF

IF EMPTY(@EndDate) THEN

    SET @EndDate = FormatDate(DateAdd(Now(),@dateOffsetEnd,'D'),'mm/dd/yyyy')

ENDIF

]%%
Day of Week Begin %%=v(@dayofWeekBegin)=%%<br>
Today %%=v(@Today)=%%<br>
Day Sub %%=v(@daySub)=%%<br>
Date Offset %%=v(@dateOffset)=%%<br>
Date Offset Begin %%=v(@dateOffsetBegin)=%%<br>
Date Offset End %%=v(@dateOffsetEnd)=%%<br>
This is the beginning of the week: %%=v(@BeginDate)=%%<br>
This is the end of the week: %%=v(@EndDate)=%%<br>

Which will output:

Day of Week Begin Mon
Today Fri
Day Sub 2
Date Offset 4
Date Offset Begin -4
Date Offset End 2
This is the beginning of the week: 09/30/2019
This is the end of the week: 10/06/2019
  • Thank you so much Gortonington!! – CAR Oct 4 at 17:41
6

Try this:

%%[
SET @LocalTime = SystemDateToLocalDate(NOW())

SET @today = FormatDate(@LocalTime, "dddd")
IF @today == "Mon" THEN
    SET @WeekStart = @LocalTime
    SET @WeekEnd = DateAdd(@LocalTime, '6','D')
ELSEIF @today == "Tue" THEN
    SET @WeekStart = DateAdd(@LocalTime, '-1','D')
    SET @WeekEnd = DateAdd(@LocalTime, '5','D')
ELSEIF @today == "Wed" THEN
    SET @WeekStart = DateAdd(@LocalTime, '-2','D')
    SET @WeekEnd = DateAdd(@LocalTime, '4','D')
ELSEIF @today == "Thu" THEN
    SET @WeekStart = DateAdd(@LocalTime, '-3','D')
    SET @WeekEnd = DateAdd(@LocalTime, '3','D')
ELSEIF @today == "Fri" THEN
    SET @WeekStart = DateAdd(@LocalTime, '-4','D')
    SET @WeekEnd = DateAdd(@LocalTime, '2','D')
ELSEIF @today == "Sat" THEN
    SET @WeekStart = DateAdd(@LocalTime, '-5','D')
    SET @WeekEnd = DateAdd(@LocalTime, '1','D')
ELSEIF @today == "Sun" THEN
    SET @WeekStart = DateAdd(@LocalTime, '-6','D') 
    SET @WeekEnd = @LocalTime
ENDIF
]%%
Today is: %%=FormatDate(@LocalTime, "MM/DD/YYYY", "", "en-US")=%%<br>
Start of the Week is: %%=FormatDate(@WeekStart, "MM/DD/YYYY", "", "en-US")=%%<br>
End of the Week is: %%=FormatDate(@WeekEnd, "MM/DD/YYYY", "", "en-US")=%%<br>

Output:
Today is: 10/04/2019
Start of the Week is: 09/30/2019
End of the Week is: 10/06/2019

Reference:
FormatDate
DateAdd
SystemDateToLocalDate

  • This is a great solution and thank you for providing an alternative to alleviate the gap from my incorrect code. – Gortonington Oct 4 at 13:31
  • Thank you Cameron!! – CAR Oct 4 at 17:40
  • I used to use this method, works great because you can't really do enumerations in ampscript. – Dave Oct 4 at 20:05

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.