0

Is it possible to have AMPScript in a Marketing Cloud email to output a month depending on the day of month from a date field in a data extension?

Eg: if the day is <=19 (say 15 May) of the month show current Month (May), >=20 (say 23 May) of the month show following month (June).

Im guessing it needs a range included: Based on if the email is being sent in May the calculation would be based on:

(>=20 April to <= 19 May) Show May (>=20 May to <= 19 June) Show June

We need to this to let people know when they will be billed on their account

I have a data extension called (BillingSetup) that has a CreatedDate which i would like to use to make the calculation but have no idea where to start

4

Can probably handle this with some DatePart and DateAdd footwork. Something like below:

%%[
SET @date = NOW()
SET @currentDay = DatePart(@date,"D")

IF @currentDay < 20 THEN
   SET @month = DatePart(@date,"monthName")
ELSE
   SET @month = DatePart(DateAdd(@date,1,"M"),"monthName")
ENDIF
]%%

%%=v(@month)=%%

This will output current month is day is lower than 20, but if the day is 20 or higher, it will output the next month.

E.g.
If date is 8/7/2020, it would output: August

If date is 8/20/2020, it would output: September

You would just adjust the value filling @date to your lookup or attributevalue() call. You would need to make sure that the date you are pulling in is actually formatted as a date, or it will cause issues. If you need to format your value, try using FormatDate.


EDIT
With DateAdd as requested in comments:

%%[
SET @date = NOW()
SET @date = DateAdd(@date, 18, "H")
SET @currentDay = DatePart(@date,"D")

IF @currentDay < 20 THEN
   SET @month = DatePart(@date,"monthName")
ELSE
   SET @month = DatePart(DateAdd(@date,1,"M"),"monthName")
ENDIF
]%%

%%=v(@month)=%%

This will now set it to 18 hours 'in the future' of server time. If you need it to be adjusted to 18 hours prior, you just change it to -18 and that will adjust accordingly.

If you noticed in my DateAdd, I have the 3rd Parameter as 'H' this is representing that hours are what will be adjusted. You had an 'M' there which means it would have been 18 months added on.

If you need help learning, I would highly recommend utilizing AMPScript.Guide and other similar resources (There are great articles and information at HowToSFMC.com as well as a good list of other resources that can be accessed for information, including blogs, videos, etc.) as well as the Email-Geeks Slack channel (which has a SFMC channel or two.

7
  • Thanks @gortonington, the only challenge i have is we have a 18hr time difference so need to do Dateadd (i think), I tried this but my AMPScript skills are lacking ` %%[ SET @date = DateAdd(now(), '18','h') SET @currentDay = DatePart(@date,"D") IF @currentDay <20 THEN SET SET @month = DatePart(@date,"monthName") ELSE SET @month = DatePart(DateAdd(@date,1,"M"),"monthName") ENDIF ]%% %%=v(@month)=%% ` – Andyw Aug 9 '20 at 21:35
  • @Andyw - Edited my answer to address your comment. – Gortonington Aug 9 '20 at 21:45
  • Thanks @gortonington, I can see the changes however i get an error " Error 1: Script SET Statement Invalid<br>The variable name is not a valid because it is of zero length. Variable names must begin with the @ symbol followed by one or more letters, numbers, or underscores. Variable Name:". I copied your exact code to make sure there is no HTML Code breaking it and its in a stand alone content block for testing. Any ideas? – Andyw Aug 9 '20 at 22:43
  • I think it was likely because I didn't put a space in the comparison and had it be <20 when it should have been < 20. Try it now – Gortonington Aug 9 '20 at 23:39
  • Its still not happy, same error @gortonington – Andyw Aug 10 '20 at 0:01

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.